pytest 如何 运行 测试/django

pytest how to run tests / django

我第一次尝试在我新创建的应用程序中使用 pytest,但我在使用 运行 时遇到了问题。我仍然收到 'no tests ran in...' 消息。

我的项目名称是 PROJECT,然后我的应用名为 diet_app,应用文件夹中的文件为 tests.py

如何运行测试?

我试过:

更准确地说 - 安装 PyTest 我使用了:

pip install pytest
pip install pytest-django

我在PyCharm

工作

pytest-django 实际上并没有在开箱即用的名为 tests.py 的文件中找到测试。如果您想继续使用名为 tests.py you'll need to add a pytest.ini file in your top-level directory:

的文件

My tests are not being found. Why?

By default, pytest looks for tests in files named test_*.py (note that this is not the same as test*.py) and *_test.py. If you have your tests in files with other names, they will not be collected. Note that Django’s startapp manage command creates an app_dir/tests.py file. Also, it is common to put tests under app_dir/tests/views.py, etc.

To find those tests, create a pytest.ini file in your project root and add an appropriate python_files line to it:

[pytest]
python_files = tests.py test_*.py *_tests.py