【问题标题】:Pytest not collecting tests with pytest commandPytest 不使用 pytest 命令收集测试
【发布时间】:2021-12-30 20:40:50
【问题描述】:

我的Project/tests/learn_pytest.py 中有一个基本测试。这是文件中的所有内容:

import pytest
import os

class Learning(tmpdir):
    def create_file(self):
        p = tmpdir.mkdir("sub").join("hello.txt")
        p.write("content")
        print tmpdir.listdir()
        assert p.read() == "content"
        assert len(tmpdir.listdir()) == 1
        assert 0

在命令行上,无论我是在 Project 中还是在 cd 中进行测试,当我运行 pytest 时,它都会输出“collected 0 items”。如果我在测试目录中并执行pytest -q learn_pytest.py,也会发生同样的事情。我在这里错过了什么?

【问题讨论】:

  • 我实际上没有定义 tmpdir,但在示例中(docs.pytest.org/en/2.8.7/tmpdir.html)他们也没有在任何地方定义它。
  • 根据pytest测试发现规则:被认为是测试,模块名必须以test_开头,类名必须以Test开头,方法名必须以@开头987654329@(所有规则都可以自定义)。不要从tmpdir 扩展,它是一个固定装置而不是一个类。相反,将 tmpdir 作为测试函数 arg 传递,例如def test_create_file(self, tmpdir): ...

标签: python pytest


【解决方案1】:

您的模块(learn_pytest.py)和方法(create_file)必须符合pytest默认命名约定(即:测试文件和函数必须以test_为前缀)

因此,例如将文件重命名为test_learn.py,将方法重命名为test_create_file

greg@canon:~/tmp/54486852$ echo "def foo(): assert 0" > foo.py
greg@canon:~/tmp/54486852$ pytest -v
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.6.7, pytest-4.0.0, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/greg/tmp/54486852, inifile:
plugins: devpi-server-4.7.1
collected 0 items                                                                                                                                                                                                 

========================================================================================== no tests ran in 0.00 seconds ===========================================================================================
greg@canon:~/tmp/54486852$ echo "def test_foo(): assert 0" > test_foo.py

greg@canon:~/tmp/54486852$ pytest -v --collect-only
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.6.7, pytest-4.0.0, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/greg/tmp/54486852, inifile:
plugins: devpi-server-4.7.1
collected 1 item                                                                                                                                                                                                  
<Module 'test_foo.py'>
  <Function 'test_foo'>

=============================================================================================
greg@canon:~/tmp/54486852$ 

【讨论】:

    【解决方案2】:

    这里是:

    文件test_learn_pytest.py

    
    def test_create_file(tmpdir):  # tmpdir is a fixture and must be a function parameter
        assert 0, "now that will fail ; change the body to what you want"
    
    
    

    【讨论】:

    • 没办法把它放在课堂上吧?
    【解决方案3】:

    将类名 Learning 更改为 TestLearning 并将函数名 create_file 更改为 test_create_file,它在我的情况下有效。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案4】:

    您缺少的可能是有关 pytest 如何搜索和查找要运行的测试的一些背景知识,即所谓的“测试发现”。

    为了简短的回答,将文件重命名为test_learn_pytest.py,将类重命名为:class TestLearning(tmpdir):,并将函数重命名为:def test_create_file

    在 pytest 中有命名函数和类的约定,由 pytest 发现。此处涵盖了约定:https://docs.pytest.org/en/6.2.x/goodpractices.html#test-discovery

    它可以被总结,我从文档中引用,经过一些修改:

    引用开始==>

    • 如果未指定参数,则(测试)集合从测试路径(如果已配置)或当前目录(如您的情况)开始。或者,可以在目录、文件名或节点 ID 的任意组合中使用命令行参数。

    • 递归到目录,除非它们匹配 norecursedirs(默认为 "")

    • 在这些目录中,搜索 test_*.py 文件或 *_test.py 文件,按其测试包名称导入。

    • 从这些文件中,收集测试项目:

      • “test”前缀的类外测试函数或方法

      • “test”前缀的测试函数或“Test”前缀的测试类中的方法(没有__init__方法)

    这是默认设置,但可以使用项目根目录下的配置文件pytest.ini 轻松更改,其内容如下...请注意,pytest 默认查找pytest.ini

    # pytest.ini
    [pytest]
    # test discovery
    python_files = test_* # all python files that starts with test_
    python_classes = Test* # all python classes that starts with Test
    python_functions = test_* # all python functions that starts with test_
    
    # do not search for tests recursively in these folders (space or newline separated)
    # norecursedirs = venv virtualenv
    
    # search only in these folders (uncomment if you really want that)
    # testpaths =
    #    unit
    #    functional
    #    integration
    

    您可以在此处阅读有关使用 ini 文件自定义 pytest 行为的更多信息:https://docs.pytest.org/en/6.2.x/customize.html

    最后,使用“pytest.mark”装饰器也很有帮助。从长远来看,它们将使您的生活更轻松并为您提供帮助。在此处阅读有关它们的更多信息:https://docs.pytest.org/en/6.2.x/example/markers.html#marking-test-functions-and-selecting-them-for-a-run

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多