【问题标题】:'pytest' exits with no error, but with "collected 0 items"'pytest' 退出时没有错误,但“收集到 0 个项目”
【发布时间】:2016-09-18 03:33:24
【问题描述】:

我一直在尝试在 Python 中使用 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_sortSorts下的方法。

【问题讨论】:

  • 请发帖minimal reproducible example。如果您放置一个空的.py 文件,您会期待什么结果?
  • 随机内存 - 如果.py 是可执行的,则会被跳过 - 查看它是否是,或者将其更改为不可执行,或者查看pytest 是否有include executable 选项?跨度>
  • @祖蓝,在我的帖子中添加了示例代码

标签: python unit-testing


【解决方案1】:

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

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

此测试收集行为can be changed 以满足您的需求。更改它需要了解configuration in pytest

【讨论】:

  • 非常感谢!是的,这有效。天哪,我从来没有意识到这一点!
  • 很高兴我能提供帮助。您介意将此标记为正确答案吗?谢谢!
  • 这个答案中的两个链接现在都是错误 404 :(
【解决方案2】:

我遇到了同样的问题,但我的函数名为 test.py。我从没想过问题会出在文件名上。

在文档中它说:

pytest 将运行当前目录及其子目录中test_*.py*_test.py 形式的所有文件。更一般地说,它遵循标准的测试发现规则。

没错!该名称应为 test_.pytest_something.py 并为我工作。

觉得自己好傻,呵呵。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      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(),现在我得到了成功的结果!

      【讨论】:

        【解决方案5】:

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

        首先,根据上述内容,我将文件重命名为“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_ 的部分,并传递了一个有用的错误消息。因此,通过这个简单的示例,事情正在按应有的方式运行。感谢所有帮助我整理的人。

        【讨论】:

          【解决方案6】:

          我也遇到了这个问题。而且它不是由脚本名称引起的,而是由于一个名为“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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-06-14
            • 2023-03-13
            • 1970-01-01
            • 2019-03-05
            • 1970-01-01
            相关资源
            最近更新 更多