关于python:使用manage.py和pytest-django进行“找不到文件”

“File not found” using manage.py with pytest-django

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
.
├── 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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中添加以下行:

1
TEST_RUNNER = 'lucy_web.runner.PytestTestRunner'

最后,pytest.ini文件与docs中的示例相同:

1
2
3
4
5
# -- 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似乎无法找到测试。 如果我运行命令

1
python manage.py test lucy_web.tests

我懂了

1
2
3
4
5
6
7
(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行并运行相同的命令,则测试将成功运行:

1
2
3
4
5
6
7
8
9
(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