为什么 PyTest 不收集测试(收集了 0 项)?

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

我一直在尝试使用 pytest 运行单元测试。

我编写了一个包含一个类和该类中的一些方法的模块,并为此模块编写了一个单元测试(使用一个简单的断言语句来检查列表的相等性),其中我首先使用列表实例化了该类。

然后我调用该对象上的方法(来自类)。

test.py
和要测试的脚本都在同一个文件夹中。当我在其上运行
pytest
时,它报告“已收集 0 个项目”。

我是

pytest
的新手,但我无法成功运行他们的示例。我在这里缺少什么?

我在 Windows 7 上运行 Python 版本 3.5.1 和 pytest 版本 2.8.1。

我的test.py代码:

from sort_algos import Sorts

def integer_sort_test():
    myobject1 = Sorts([-100, 10, -10])
    assert myobject1.merge_sort() == [-101, -100, 10]

sort_algos.py
是一个包含
Sorts
类的模块。

merge_sort
是来自
Sorts
的方法。

python unit-testing pytest
9个回答
190
投票

pytest
根据命名约定收集测试。 默认情况下,任何包含测试的文件都必须以
test_
开头命名,包含测试的类必须以
Test
开头命名,并且文件中应被视为测试的任何函数也必须以
test_ 开头

如果您将测试文件重命名为

test_sorts.py
,并将上面提供的示例函数重命名为
test_integer_sort
,那么您会发现它会自动收集并执行。

此测试收集行为可以更改以满足您的愿望。更改它需要了解 pytest 中的配置


19
投票

我有同样的问题,但我的函数被称为

test.py
。我从没想过问题会出在文件名上。

在文档中写道:

pytest 将运行当前目录及其子目录中

test_*.py
*_test.py
形式的所有文件。更一般地说,它遵循标准测试发现规则。

完全正确!名称应该是

test_.py
test_something.py
并且适合我。

我觉得自己好傻,呵呵。


7
投票

我还不能投票,但想加入一个对我有用的组合解决方案。

首先,根据上面的内容,我将文件重命名为“test_”:

test_hello.py

其次,同样根据上面,我用“test_”重命名了我想测试的每一位

def test_greeting():
    print('Hello, world!')

def test_checkout():
    print("This is checkout")

def logout():
    print("This is logout")

def test_executable():
    """says hello world by default"""
    out = getoutput({prg})

最后,为了实际运行测试,我在命令行中输入了以下内容:

python -m pytest -v test_hello.py

如果没有

-m
,则会出现一条消息,无法打开文件。它可以在没有
-v
的情况下工作,但有了它,它就会分解每个正在测试的部分。结果:

---------------------------------------------
collected 3 items

test_hello.py::test_greeting PASSED                                                                              [ 33%]
test_hello.py::test_checkout PASSED                                                                              [ 66%]
test_hello.py::test_executable FAILED


def test_executable():
        """says hello world by default"""
>       out = getoutput({prg})
E       NameError: name 'getoutput' is not defined

test_hello.py:12: NameError

如您所见,它跳过了

logout()
(程序中没有 test_ 的部分),并传递了一条有用的错误消息。因此,通过这个简单的例子,一切都按预期运行。感谢所有帮助我整理它的人。


3
投票

我也遇到了这个问题,我花了一整天才发现类名应该以“Test”开头,我的类名是“test”。这使我成功运行了我的代码。

确保你的Python测试文件名和方法名也应该以“test_”开头


2
投票

这对我帮助很大。最初我将我的测试命名如下:

def login():
    print("This is login")

def checkout():
    print("This is checkout")

def logout():
    print("This is logout")

我的 pytest 命令运行没有错误。然而,没有产生任何结果。我还观察到:

已收集 0 件物品

因此我将上述测试重命名为

test_login()
test_checkout()
test_logout()
,现在我得到了成功的结果!


0
投票

我也遇到了这个问题。这不是由脚本名称引起的,而是由一个名为“pytest-appium”的插件引起的,我一开始就不打算使用它。 我终于从报告中发现了: ('c:\python39\lib\site-packages\pytest_appium\plugin.py', 8, '跳过:无变量文件')

因为

plugin.py

需要变量,我不知道它的用途。 @pytest.fixture(scope='session') def appium(variables): if not variables: pytest.skip('no variables file')

解决方案:

pip uninstall pytest-appium



0
投票

已收集 0 件物品

======================== 在 X.XX 中没有运行测试==================== ===

再次保存文件(在 VSCode 中)以更新文件


0
投票
poetry run pytest

运行测试时遇到“已收集 0 个项目”错误。

我的测试文件位于项目文件夹根部的

tests

目录中。

将以下行添加到 

pyproject.toml

文件解决了问题:

[tool.pytest.ini_options]
testpaths = ["tests"]



0
投票
conftest.py

但忘记修复

pytest.ini
文件。 任何遇到
Collected 0 items
困扰的人都可以尝试修复
pytest.ini
文件。
    

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