pytest doesn't run any tests and it is not clear why. I tried to use --debug but didn't get any valuable information. It is completely not clear how to debug this sort of issues with pytest. (Looks like some issue with pytest configuration / env variables / test names patterns?)
PYTEST不运行任何测试,原因尚不清楚。我试图使用--DEBUG,但没有得到任何有价值的信息。完全不清楚如何用PYTEST调试这类问题。(看起来像是关于pytest配置/环境变量/测试名称模式的一些问题?)
Example of test file:
测试文件示例:
import pytest
@pytest.mark.sanity
def test_me():
"""I'm a test."""
assert True
But pytest doesn't run any test, why?
但是pytest不运行任何测试,为什么?
$ pytest
================================================== test session starts ===================================================
platform linux2 -- Python 2.7.12, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/qawizard/pytest-hell, inifile:
plugins: xdist-1.15.0, timeout-1.2.0
collected 1 item s
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Exit: Done! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================== no tests ran in 0.13 seconds ==============================================
更多回答
did you register that marker in pytest.ini
? do you have any OTHER configuration in pytest.ini
? could you rerun pytest -lsvvv
to see extra verbose output? do you have conftest.py
with some custom configuration?
您是否在pyest.ini中注册了该标记?您在pyest.ini中有任何其他配置吗?您是否可以重新运行pytest-lsvvv以查看额外的详细输出?你们有带定制配置的confest.py吗?
To determine why tests are not running, these steps are useful:
要确定测试未运行的原因,以下步骤很有用:
- Verify that all files with test cases have a 'test_' prefix.
- Verify that all test case names also have a 'test_' prefix.
- Verify that you have created
pytest.ini
file in the root directory.
- Verify that you have
__init__.py
file in all directories/sub-directories of the project.
You need all tests to start with test_
but also you need to tell Pytest exact which files to look for:
您需要所有测试都以test_开头,但您还需要告诉Pytest确切要查找哪些文件:
# pytest.ini
[pytest]
DJANGO_SETTIONS_MODULE = myproject.settings
python_files = tests.py test_*.py
For me my class name didn't start with test.
对我来说,我的类名不是以test开头的。
my code was
我的代码是
class MainTest:
...
def test_properties(self):
...
This wont work because pytest will assume this class shouldn't be included.
Changing to this did it for me.
这是行不通的,因为pytest会假定不应该包含这个类。换成这个对我来说是有好处的。
class TestMain:
...
def test_properties(self):
...
For example, there is apples/apple.py
as shown below:
例如,有苹果/apple.py,如下所示:
project
|-pytest.ini
└-apples
|-__init__.py
└-apple.py # Here
Then, there is apple(self)
in Apple
class in apples/apple.py
as shown below:
然后,在Apple/apple.py中的Apple类中有Apple(Self),如下所示:
# "apples/apple.py"
import pytest
class Apple:
def apple(self):
assert True
Now, with this pytest.ini
below, apple(self)
cannot be run because by default, Pytest can run the files test_*.py
and *_test.py
, the classes prefixed with Test
and the functions prefixed with test
instead of apple.py
file, Apple
class and apple(self)
function according to python_files, python_classes and python_functions and by default, Pytest can run any folders like apples
:
现在,有了下面的pyest.ini,Apple(Self)就不能运行了,因为默认情况下,Pytest可以运行文件test_*.py和*_test.py,以及以test为前缀的类和前缀为test的函数,而不是以apple.py文件、Apple类和Apple(Self)函数为前缀。默认情况下,Pytest可以运行任何文件夹,如Apple:
# "pytest.ini"
[pytest]
So, with this pytest.ini
below, apple(self)
can be run:
因此,使用下面的pyest.ini文件,可以运行Apple(Self):
# "pytest.ini"
[pytest]
python_files = apple.py
python_classes = Apple
python_functions = apple
In addition, by default, you can run all the tests in apples/test_1.py
, apples/oranges/test_2.py
, bananas/test_1.py
and banana/kiwis/test_2.py
as shown below because as I said above, by default, Pytest can run any folders like apples
, oranges
, bananas
and kiwis
. *My answer explains it:
此外,在默认情况下,您可以运行Apple/test_1.py、Apple/oranges/test_2.py、bananas/test_1.py和banana/kiwis/test_2.py中的所有测试,如下所示,因为正如我在上面所说的,在默认情况下,Pytest可以运行任何文件夹,如Apple、Oranges、Banana和Kiwis。*我的回答解释了这一点:
project
|-pytest.ini
|-apples
| |-__init__.py
| |-test_1.py # Here
| └-oranges
| |-__init__.py
| └-test_2.py # Here
└-bananas
|-__init__.py
|-test_1.py # Here
└-kiwis
|-__init__.py
└-test_2.py Here
更多回答
What is DJANGO_SETTIONS_MODULE and why we use it?
什么是Django_SETTIONS_MODULE?我们为什么要使用它?
我是一名优秀的程序员,十分优秀!