Search code examples
djangopytestcode-coveragedjango-testingpytest-cov

Not able to run the coverage with pytest-cov and pytest


Facing issues in running the pytest with coverage, I have gone through the SO posts and not able to fix this, I believe I'm missing something here..

Getting the following errors where users is an app of my project

ModuleNotFoundError: No module named 'users'

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

My pytest.ini file contents

[pytest] DJANGO_SETTINGS_MODULE = cloudstack.settings
python_files = tests.py test_*.py *_test.py
addopts = -v --ignore=venv --cov=. --cov-report=html

I have tried adding DJANGO_SETTINGS_MODULE as environment variable too then I'm getting different error saying that cloudstack module is not found.

I'm in the activated environment while performing these tests.

EDIT I looked into every post of the stack overflow to fix the errors in running the pytest with coverage none of them helped me in resolving this.

Django upgrading to 1.9 error "AppRegistryNotReady: Apps aren't loaded yet."

PATH issue with pytest 'ImportError: No module named YadaYadaYada'

ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

And I had everything aligned as per the instructions in the SO posts'

$ type -a coverage coverage is /cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/coverage

$ type -a pytest pytest is /cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/pytest

$ type -a python python is /cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/python

sys.path ['/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cloudstacko', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/lib/python3.6/site-packages']


Solution

  • The library which saved my day.

    $ pip install pytest-django