nedbat/django_coverage_plugin

Tests for template tags with pytest-xdist and pytest-cov break view tests using the template tags

TauPan opened this issue ยท 7 comments

See pytest-dev/pytest-cov#285

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 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.

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?

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

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.

I'm sorry this repo has been so quiet. We don't have an active maintainer at the moment.

This is fixed in 6622791.

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!