繁体   English   中英

Pytest 不使用 pytest 命令收集测试

[英]Pytest not collecting tests with pytest command

我在我的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 in 进行测试,当我运行pytest ,它都会输出“collected 0 items”。 如果我在测试目录中并执行pytest -q learn_pytest.py会发生同样的事情。 我在这里错过了什么?

您的模块(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$ 

这里是 :

文件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"


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

您缺少的可能是有关 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM