Description
Here's a repository that reproduces the behaviour (see README):
https://github.com/TauPan/pytest-django-xdist-cov-bug
(Which also reproduces #36)
General description:
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.
The error only appears if both -n
and --cov
are used.
There are two workarounds at this point:
- Move the business logic for custom template tags and filters into a separate module and test separately.
- Explicitly import the template tag library as proposed in Tests for template tags with pytest-xdist and pytest-cov break view tests using the template tags pytest-dev/pytest-cov#285 (comment)
However the django documentation at 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.
So my take would be that discovery of template tag libraries should not depend on the presence of --cov
and -n
.