【问题标题】:Not able to run the coverage with pytest-cov and pytest无法使用 pytest-cov 和 pytest 运行覆盖范围
【发布时间】:2020-11-17 18:31:07
【问题描述】:

在运行覆盖范围的 pytest 时遇到问题,我浏览了 SO 帖子但无法解决这个问题,我相信我在这里遗漏了一些东西..

在用户是我项目的应用的情况下出现以下错误

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.

我的 pytest.ini 文件内容

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

我也尝试将 DJANGO_SETTINGS_MODULE 添加为环境变量,然后我得到不同的错误,说 cloudstack module is not found

我在执行这些测试时处于激活的环境中。

编辑 我查看了堆栈溢出的每一篇文章,以修复运行 pytest 时出现的错误,但它们都没有帮助我解决这个问题。

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

我按照 SO 帖子中的说明将所有内容对齐

$ type -a 覆盖范围是 /cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/coverage

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

$ type -a python python 是 /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']

【问题讨论】:

    标签: django pytest code-coverage django-testing pytest-cov


    【解决方案1】:

    图书馆拯救了我的一天。

    $ pip install pytest-django

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 2020-01-05
      • 2018-07-17
      • 2022-06-11
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      相关资源
      最近更新 更多