2

I am using Coverage to measure my test code, but I stuck on a weird problem. I tried the command according to Django Documentation:

coverage run --source='.' manage.py test myapp

But I got this error:

Coverage.py warning: No data was collected. (no-data-collected) Couldn't run 'manage.py' as Python code: SyntaxError: invalid syntax (manage.py, line 16)

It works correctly using python manage.py test myapp.

Here is my manage.py (I didn't modify anything).

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytestsite.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

What am I missing exactly?

I ran my code and coverage in virtualenv, but I don't think this could be a problem.

ENV:

Python 3.6.8
Django 2.2.3
Coverage 4.5.4
5
  • 1
    Coverage is using Python 2 for some reason. How did you install it? Commented Sep 12, 2019 at 8:28
  • I created a virtual env python3 -m virtualenv .env and source, and then I can run coverage. Commented Sep 12, 2019 at 8:37
  • 1
    That command won't install coverage. What does "which coverage" show for you? Commented Sep 12, 2019 at 10:01
  • I type which coverage in virtualenv, it shows /usr/local/bin/coverage. Commented Sep 16, 2019 at 0:18
  • 1
    @Corey that result means you didn't install coverage in that virtual env, and it's using the one in /usr/local/bin instead, which is apparently running python version 2. Commented Sep 26, 2023 at 15:54

1 Answer 1

4

By default, coverage may be using python2 instead of python 3. You can use:

coverage3 run --source='.' manage.py test myapp
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.