python - "File not found"将 manage.py 与 pytest-django 一起使用

标签 python django pytest pytest-django

我有一个 Django 项目,其中某些环境变量在 manage.py 中设置,稍后用作 settings.py 中定义的值。因此,为了运行 pytest-django ,我想先运行 manage.py

我正在尝试按照 https://pytest-django.readthedocs.io/en/latest/faq.html#how-can-i-use-manage-py-test-with-pytest-django 中的说明进行操作,但我遇到了意外错误。我有以下目录结构:

.
├── lucy
│   ├── settings
    │   ├── base.py
    │   ├── development.py
    │   ├── production.py
│   └── staging.py
│   ├── staticfiles
│   ├── urls.py
│   └── wsgi.py
├── lucy_web
│   ├── __init__.py
│   ├── actions.py
│   ├── admin
│   ├── apps.py
│   ├── fixtures
│   ├── forms
│   ├── lib
│   ├── management
│   ├── migrations
│   ├── models
│   ├── runner.py
│   ├── serializers.py
│   ├── static
│   ├── templates
│   ├── templatetags
│   ├── tests
│   ├── urls.py
│   └── views
├── manage.py
├── pytest.ini

runner.py 的内容完全取自 FAQ:

class PytestTestRunner(object):
    """Runs pytest to discover and run tests."""

    def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs):
        self.verbosity = verbosity
        self.failfast = failfast
        self.keepdb = keepdb

    def run_tests(self, test_labels):
        """Run pytest and return the exitcode.

        It translates some of Django's test command option to pytest's.
        """
        import pytest

        argv = []
        if self.verbosity == 0:
            argv.append('--quiet')
        if self.verbosity == 2:
            argv.append('--verbose')
        if self.verbosity == 3:
            argv.append('-vv')
        if self.failfast:
            argv.append('--exitfirst')
        if self.keepdb:
            argv.append('--reuse-db')

        argv.extend(test_labels)
        return pytest.main(argv)

最后,在 lucy/settings/base.py 中添加以下行:

TEST_RUNNER = 'lucy_web.runner.PytestTestRunner'

最后,pytest.ini 文件如文档中的示例所示:

# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = lucy.settings.production
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py

问题是 pytest 似乎无法找到测试。如果我运行命令

python manage.py test lucy_web.tests

我明白了

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py test lucy_web.tests
============================================ test session starts ============================================
platform darwin -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /Users/kurtpeek/Documents/Dev/lucy/lucy-web, inifile: pytest.ini

======================================= no tests ran in 0.00 seconds ========================================
ERROR: file not found: lucy_web.tests

但是,如果我注释掉 base.py 中的 TEST_RUNNER 行并运行相同的命令,测试将成功运行:

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py test lucy_web.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
....E.F................
----------------------------------------------------------------------
Ran 23 tests in 15.974s

FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...

我在这里做错了什么?我需要将 runner.py 放在不同的位置吗?

最佳答案

可能是因为__init__.py

我在使用pytest的时候也遇到过类似的情况。我删除了测试目录中的 __init__.py,它测试得很好。

情况有点不同,但我希望这会有所帮助。

links that I got help.

https://docs.pytest.org/en/latest/pythonpath.html

`py.test` and `__init__.py` files

关于python - "File not found"将 manage.py 与 pytest-django 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179315/

相关文章:

python - 为单个单元测试用例更改 celery 设置 task_always_eager

python - Python中使用tempfile和subprocess将mysql转入mongodb

mysql - 什么是 SELECT col1 FROM table WHERE col2=somestring 的 Django ORM 等价物?

python - Unicode解码错误: 'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte

pytest - 从 pytest 导出 HTML 报告的当前方法是什么?

python - 在 pytest 的测试目录中查找文件夹的最佳方法

python - 具有多个属性级别的类 [初学者]

python - LightGBM 错误 : ValueError: For early stopping, 至少需要一个数据集和评估指标进行评估

python - 在 Django 中创建一个智能类工厂

sql - 如何在Django中每天为数据库创建新表