'pytest' 退出时没有错误,但带有“收集 0 个项目"; [英] 'pytest' exits with no error, but with "collected 0 items"

查看:58
本文介绍了'pytest' 退出时没有错误,但带有“收集 0 个项目";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Python 中的 pytest 运行单元测试.我编写了一个模块,其中包含一个类和该类中的一些方法.我为这个模块写了一个单元测试(用一个简单的断言语句来检查列表的相等性),我首先用一个列表实例化这个类.然后我调用该对象上的一个方法(从类中).test.py 和要测试的脚本都在同一个文件夹中.当我在其上运行 pytest 时,我得到收集 0 个项目".

I have been trying to run unit tests using pytest in Python. I had written a module with one class and some methods inside that class. I wrote a unit test for this module (with a simple assert statement to check equality of lists) where I first instantiate the class with a list. Then I invoke a method on that object (from the class). Both test.py and the script to be tested are in the same folder. When I run pytest on it, I get "collected 0 items".

我是 pytest 的新手,但我无法成功运行他们的示例.我在这里错过了什么?

I am new to pytest, and but I am unable to run their examples successfully. What am I missing here?

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

I am running Python version 3.5.1 and pytest version 2.8.1 on Windows 7.

我的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_sortSorts 下的一个方法.

sort_algos.py is a module containing class Sorts. merge_sort is a method under Sorts.

推荐答案

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

pytest gathers tests according to a naming convention. By default any file that is to contain tests must be named starting with test_, classes that hold tests must be named starting with Test, and any function in a file that should be treated as a test must also start with test_.

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

If you rename your test file to test_sorts.py and rename the example function you provide above as test_integer_sort, then you will find it is automatically collected and executed.

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

This test collecting behavior can be changed to suit your desires. Changing it will require learning about configuration in pytest.

这篇关于'pytest' 退出时没有错误,但带有“收集 0 个项目";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆