英文:
PyTest: "no tests ran" even though tests were clearly run
问题
我有以下的pytest
配置文件:
[pytest]
filterwarnings =
ignore::DeprecationWarning
ignore::UserWarning
python_files =
test_file_1.py
test_file_2.py
log_cli = True
log_level = INFO
我在本地使用以下命令运行pytest
:
python -m pytest path
并得到如下输出:
====================================================================================================== no tests ran in 24.77s =======================================================================================================
这个输出令我难以置信,因为之前,由于有日志记录,我在终端中会得到以下输出:
INFO root:test_file_1.py:40 torch.Size([100, 71])
我注意到,如果我在pytest.ini
中删除filterwarnings
,我会得到一个"警告汇总"的输出,而不是"没有运行测试"的输出。
这里到底发生了什么?为什么filterwarnings
会导致"没有运行测试"的输出?
英文:
I have the following config file for pytest
:
[pytest]
filterwarnings =
ignore::DeprecationWarning
ignore::UserWarning
python_files =
test_file_1.py
test_file_2.py
log_cli = True
log_level = INFO
I run pytest
locally with the following command:
python -m pytest path
and get the output
====================================================================================================== no tests ran in 24.77s =======================================================================================================
which I cannot believe, since before, I get the following output in the terminal (because of the logging):
INFO root:test_file_1.py:40 torch.Size([100, 71])
I noticed that if I remove the filterwarnings
in pytest.ini
, I get a warnings summary
, but not the output "no tests ran".
What exactly is happening here? Why does filterwarnings
lead to the output "no tests ran"?
答案1
得分: 2
pytest
期望测试方法按照特定的方式命名。例如,def test_1():
。
如果你只是将 Python 代码放入一个文件中,并使用 pytest
运行它:
import time
time.sleep(2)
你会看到:2.01 秒内没有运行任何测试
。
但是,如果你的文件中有一个以 test_
开头的方法,并使用 pytest
运行它:
import time
def test_1():
time.sleep(2)
你会看到:2.01 秒内运行了 1 个测试,全部通过
。
因此,如果你希望你的测试被发现,就重要遵循 pytest
设定的命名约定。(有关发现规则的更多信息:https://docs.pytest.org/en/latest/example/pythoncollection.html)
请确保你的文件以 test_
开头,并且你的方法以 test_
开头,这样默认的 pytest
发现规则就能找到你的测试。
英文:
pytest
expects test methods to be named a certain way. Eg def test_1():
.
If you just throw python code into a file and run it with pytest
:
import time
time.sleep(2)
You'll see: no tests ran in 2.01s
.
But if you have a method that starts with test_
in your file and run it with pytest
:
import time
def test_1():
time.sleep(2)
You'll see: 1 passed in 2.01s
.
So it's important to follow the naming conventions set by pytest
if you want your tests to be discovered. (More info on discovery rules: https://docs.pytest.org/en/latest/example/pythoncollection.html)
Have your files start with test_
, and have your methods start with test_
so that the default pytest
discovery rules find your tests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论