Tests for template tags with pytest-xdist and pytest-cov break view tests using the template tags
See original GitHub issueThis was a weird one for me to figure out, but it should be simple to reproduce:
1.) You have a template tag library “foo”
2.) You have a view using a template using that library via load foo
3.) Being a thorough tester, you decide you need a test for the template tag library, which means you have to from app.templatetags import foo
in your test.
4.) And of course you need to test the view using the templatetag.
5.) And maybe you have to test the templatetag before the view (not sure if this is relevant) e.g. pytest discovery puts it before the view test.
6.) And since you have many tests, you run pytest --cov -n 2
Which results in an error like the following:
django.template.exceptions.TemplateSyntaxError: 'foo' is not a registered tag library.
(maybe related to #110, but I don’t see any of the problems described in that issue)
Workaround:
- You put the business code you want to test in a different module (one that is not imported from a template) and register the tags manually via
from django import template
from ._foo import (tag1, tag2)
register = template.Library()
register.filter(is_safe=True)(tag1)
register.tag()(tag2)
And of course you import _foo
in your test, instead of the template library.
I’m not sure if I should file this with pytest-xdist or pytest-cov, since it definitely only occurs if both are used. I’m filing this here first and we can figure out if it really is xdist’s fault.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (5 by maintainers)
Top GitHub Comments
Ok so I’ve tried the code. As explained before the problem are some missing imports. Apply this to fix your project:
Ok, it appears to be as you explained in https://github.com/pytest-dev/pytest-cov/issues/285#issuecomment-488733091
in fact against current master the following patch is sufficient to not cause an error:
pytest --cov -n2
completes green with the following result:But the result raises more questions (which is probably a good thing):
1.) home.html is not only reported with coverage of 0% (probably because of https://github.com/nedbat/django_coverage_plugin/issues/36 ) but omitted completely.
2.) https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/ (or 1.11 or any version) does not mention that templatetags libraries need to be imported anywhere. I’m not sure if any of the relevant modules mentions that (pytest-cov, pytest-xdist, pytest-django or django_coverage_plugin). Since production code runs without those imports (and
--cov
and-n2
on their own as well) I suspect there’s still a bug somewhere and importing those modules explicitly is just a workaround, with the advantage that it’s simpler than my initial workaround of moving the busines code of the template tags and filters out of the tag library module and testing it separately.You seem sure that there’s no bug in pytest-cov, so I suspect there might be one in pytest-django or pytest_coverage_plugin.
In any case, thanks for your assistance!