我有以下 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
会导致输出“没有测试运行”?
最佳答案
pytest
期望测试方法以某种方式命名。例如 def test_1():
。
如果您只是将 python 代码放入文件中并使用 pytest
运行它:
import time
time.sleep(2)
您将看到:2.01 秒内未运行任何测试
。
但是,如果您的文件中有一个以 test_
开头的方法,并使用 pytest
运行它:
import time
def test_1():
time.sleep(2)
您将看到:1 在 2.01 秒内传递
。
因此,如果您希望您的测试被发现,那么遵循 pytest 设置的命名约定非常重要。 (有关发现规则的更多信息:https://docs.pytest.org/en/latest/example/pythoncollection.html)
让您的文件以 test_
开头,并让您的方法以 test_
开头,以便默认的 pytest
发现规则找到您的测试。
关于python-3.x - PyTest: "no tests ran",即使测试已明确运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76580060/