Python Pytest未使用Pytest命令收集测试

Python Pytest未使用Pytest命令收集测试,python,pytest,Python,Pytest,我的项目/tests/learn\u 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.

我的
项目/tests/learn\u 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
时,它都会输出“收集的0项”。如果我在tests目录中,执行
pytest-qlearn\u pytest.py
同样的操作。我在这里遗漏了什么?

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

例如,将文件重命名为
test\u learn.py
,将方法重命名为
test\u create\u 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$ 
greg@canon:~/tmp/54486852$echo“def foo():assert 0”>foo.py
greg@canon:~/tmp/54486852$pytest-v
======================================================================================================================================================测试会话开始===============================================================================================
平台linux——Python 3.6.7、pytest-4.0.0、py-1.7.0、pluggy-0.8.0——/usr/bin/python3.6
cachedir:.pytest\u缓存
rootdir:/home/greg/tmp/54486852,文件:
插件:devpi-server-4.7.1
收集了0个项目
==========================================================================================================================================================================================0.00秒内没有运行任何测试===========================================================================================
greg@canon:~/tmp/54486852$echo“def test\u foo():assert 0”>test\u foo.py
greg@canon:~/tmp/54486852$pytest-v--仅限收集
======================================================================================================================================================测试会话开始===============================================================================================
平台linux——Python 3.6.7、pytest-4.0.0、py-1.7.0、pluggy-0.8.0——/usr/bin/python3.6
cachedir:.pytest\u缓存
rootdir:/home/greg/tmp/54486852,文件:
插件:devpi-server-4.7.1
收集1项
=============================================================================================
greg@canon:~/tmp/54486852美元
在这里:

文件
test\u learn\u 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"



实际上,我得到的tmpdir没有定义,但在示例()中,他们也没有在任何地方定义它。根据
pytest
测试发现规则:要被视为测试,模块名称必须以
test\uuu
开头,类名必须以
test
开头,方法名称必须以
test\uu
开头(所有规则都可以自定义)。不要从
tmpdir
扩展,它是一个固定装置,而不是一个类。相反,将
tmpdir
作为测试函数arg传递,例如
def test\u create\u file(self,tmpdir):…
无法将其放入类中?