Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoverageException: Can't add file tracer data for unmeasured file #69

Closed
suda opened this issue Apr 12, 2020 · 2 comments
Closed

CoverageException: Can't add file tracer data for unmeasured file #69

suda opened this issue Apr 12, 2020 · 2 comments

Comments

@suda
Copy link

suda commented Apr 12, 2020

A combination of django, pytest, pytest-cov, django-coverage-plugin and pytest-django seems to interact with each other causing CoverageException when tests execute code that renders templates. My guess is it's somewhere in django-coverage-plugin as disabling it, removes the exception.

I created an example Django project that exhibits this issue: https://github.com/suda/pytest_django_coverage_test
Not sure if it's a bug or misconfiguration but I hope this repo will be useful for figuring out the root cause 😊

@MicahLyle
Copy link

MicahLyle commented Apr 23, 2020

I've also run into this problem with the exact same configuration you're talking about 👍

@nedbat
Copy link
Owner

nedbat commented Jul 12, 2020

An interesting bug report, thanks!

There are a few things going on here. If I add the -s switch to pytest, then I can see the first issue:

render_something/tests.py Coverage.py warning: Disabling plug-in 'django_coverage_plugin.DjangoTemplatePlugin' due to an exception:
Traceback (most recent call last):
  File "/usr/local/virtualenvs/tmp-91b59576424c64de/lib/python3.7/site-packages/coverage/inorout.py", line 263, in should_trace
    file_tracer = plugin.file_tracer(canonical)
  File "/usr/local/virtualenvs/tmp-91b59576424c64de/lib/python3.7/site-packages/django_coverage_plugin/plugin.py", line 179, in file_tracer
    self.debug_checked = check_debug()
  File "/usr/local/virtualenvs/tmp-91b59576424c64de/lib/python3.7/site-packages/django_coverage_plugin/plugin.py", line 86, in check_debug
    "Template debugging must be enabled in settings."
django_coverage_plugin.plugin.DjangoTemplatePluginException: Template debugging must be enabled in settings.

Pytest normally captures all the output, so you couldn't see this message. The plugin is being disabled, but later, coverage doesn't take that into account, and tries to do some final bookkeeping for a plugin which never recorded any lines.

You can fix the problem by enabling debugging in your template engine:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': True,       ## <--- add this line.
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

I'll have to think about how to fix coverage so that it properly tracks disabled plugins. I've written it up as nedbat/coveragepy#1011.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants