You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tests for template tags with pytest-xdist and pytest-cov break view tests using the template tags #63
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.
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.
I'm running into this issue using Django 2.2.17 and pytest /w plugins: celery-4.4.6, sugar-0.9.4, xdist-2.1.0, cov-2.10.1, django-4.1.0, Faker-4.17.1, forked-1.3.0, subtests-0.3.2 and using django_coverage_plugin.
It's a bit late but I can confirm that this fixes the problems for me.
The reason I needed some time to validate this was that I saw new errors while running coverage concurrently, which turned out to be problems in my test setup.
(I had concurrent tests using the same directory, and after introducing some setup code to fix that, I can run coverage concurrently without issues.)
Activity
izquierdo commentedon Jun 19, 2019
I was able to get the same result using your test repository and confirmed #64 fixes the issue. Would you please confirm on your end too?
jambonrose commentedon Jun 19, 2019
Hi folks,
I will look at this ASAP, but don't expect to get around too this or #64 until at least this weekend.
Thank you for your patience.
Andrew
tlrh314 commentedon Nov 24, 2020
What's the status of this issue?
I'm running into this issue using Django 2.2.17 and pytest /w plugins: celery-4.4.6, sugar-0.9.4, xdist-2.1.0, cov-2.10.1, django-4.1.0, Faker-4.17.1, forked-1.3.0, subtests-0.3.2 and using django_coverage_plugin.
nedbat commentedon Nov 24, 2020
I'm sorry this repo has been so quiet. We don't have an active maintainer at the moment.
Update history for #63
nedbat commentedon May 25, 2021
This is fixed in 6622791.
nedbat commentedon Jun 8, 2021
This is now released in v2.0.0: https://pypi.org/project/django-coverage-plugin/2.0.0/
TauPan commentedon Aug 23, 2021
It's a bit late but I can confirm that this fixes the problems for me.
The reason I needed some time to validate this was that I saw new errors while running coverage concurrently, which turned out to be problems in my test setup.
(I had concurrent tests using the same directory, and after introducing some setup code to fix that, I can run coverage concurrently without issues.)
Thanks for the fix!