简体   繁体   English

Pytest 不使用 pytest 命令收集测试

[英]Pytest not collecting tests with pytest command

I have in my Project/tests/learn_pytest.py a basic test.我在我的Project/tests/learn_pytest.py有一个基本测试。 This is everything in the file:这是文件中的所有内容:

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

On the command line, whether I am in Project or cd in to tests, when I run pytest , it outputs "collected 0 items".在命令行上,无论我是在 Project 中还是 cd in 进行测试,当我运行pytest ,它都会输出“collected 0 items”。 If I'm in the tests directory and do pytest -q learn_pytest.py the same thing happens.如果我在测试目录中并执行pytest -q learn_pytest.py会发生同样的事情。 What am I missing here?我在这里错过了什么?

your module (learn_pytest.py) and method (create_file) must conform to pytest default naming convention (which is: test files and functions must be test_ prefixed) 您的模块(learn_pytest.py)和方法(create_file)必须符合pytest的默认命名约定(即:测试文件和函数必须带有test_前缀)

So, rename the file to test_learn.py for instance and the method to test_create_file 因此,例如,将文件重命名为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$ 

Here it is : 这里是 :

file test_learn_pytest.py : 文件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,它在我的情况下有效。

What you are missing is maybe some background on how pytest search and find tests to run, what is known as "test discovery".您缺少的可能是有关 pytest 如何搜索和查找要运行的测试的一些背景知识,即所谓的“测试发现”。

For a short answer, rename the file to test_learn_pytest.py , rename the class to: class TestLearning(tmpdir): and rename the function to: def test_create_file简而言之,将文件重命名为test_learn_pytest.py ,将类重命名为: class TestLearning(tmpdir):并将函数重命名为: def test_create_file

In pytest there the convention for naming functions and classes to be discovered by pytest.在 pytest 中有命名函数和类的约定,由 pytest 发现。 The convention is covered here: https://docs.pytest.org/en/6.2.x/goodpractices.html#test-discovery此处涵盖了该约定: https : //docs.pytest.org/en/6.2.x/goodpractices.html#test-discovery

And it can be summerized and i am quoting, with some modification, from the documentation:它可以概括,我从文档中引用,经过一些修改:

begin of quote==>引用开始==>

  • If no arguments are specified then collection (of tests) starts from testpaths (if configured) or the current directory (like in your case).如果未指定参数,则(测试)集合从测试路径(如果已配置)或当前目录(如您的情况)开始。 Alternatively, command line arguments can be used in any combination of directories, file names or node ids.或者,可以在目录、文件名或节点 ID 的任意组合中使用命令行参数。

  • Recurse into directories, unless they match norecursedirs (which is "" by default)递归到目录中,除非它们匹配 norecursedirs(默认为 "")

  • In those directories, search for test_*.py files or *_test.py files, imported by their test package name.在这些目录中,搜索 test_*.py 文件或 *_test.py 文件,按其测试包名称导入。

  • From those files, collect test items:从这些文件中,收集测试项目:

    • "test" prefixed test functions or methods outside of class “test”前缀测试函数或类外的方法

    • "test" prefixed test functions or methods inside "Test" prefixed test classes (without an __init__ method) “test”前缀测试函数或“test”前缀测试类中的方法(没有__init__方法)

<==end of quote <==引用结束

This is the default but it can be easily changed using a configuration file pytest.ini at the project root with the following content... please note that pytest looks for pytest.ini by default:这是默认设置,但可以使用项目根目录下的配置文件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

you can read more on customizing the behavior of pytest using the ini file here: https://docs.pytest.org/en/6.2.x/customize.html您可以在此处阅读有关使用 ini 文件自定义 pytest 行为的更多信息: https ://docs.pytest.org/en/6.2.x/customize.html

Finally, it is helpful also make use of "pytest.mark" decorators.最后,使用“pytest.mark”装饰器也很有帮助。 They will make your life easier and help you on the long run.从长远来看,它们将使您的生活更轻松,并为您提供帮助。 Read more about them here: https://docs.pytest.org/en/6.2.x/example/markers.html#marking-test-functions-and-selecting-them-for-a-run在此处阅读有关它们的更多信息: 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