PyTest:“没有运行任何测试”,即使测试已明确运行

问题描述 投票:0回答:1

我有以下

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])

我注意到,如果删除

filterwarnings
中的
pytest.ini
,我会得到
warnings summary
,但不会输出“没有测试运行”。

这里到底发生了什么?为什么

filterwarnings
会导致输出“没有测试运行”?

python python-3.x unit-testing pytest
1个回答
2
投票

pytest
期望测试方法以某种方式命名。例如
def test_1():

如果您只是将 python 代码放入文件中并使用

pytest
运行它:

import time

time.sleep(2)

您会看到:

no tests ran in 2.01s

但是如果您的文件中有一个以

test_
开头的方法并使用
pytest
运行它:

import time

def test_1():
    time.sleep(2)

您会看到:

1 passed in 2.01s

因此,如果您希望您的测试被发现,遵循

pytest
设置的命名约定非常重要。 (有关发现规则的更多信息:https://docs.pytest.org/en/latest/example/pythoncollection.html

让您的文件以

test_
开头,让您的方法以
test_
开头,以便默认的
pytest
发现规则找到您的测试。

© www.soinside.com 2019 - 2024. All rights reserved.