我的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在命令行上,无论我是在项目中还是在cd中进行测试,当我运行pytest时,它都会输出"collected 0 items“。如果我在pytest -q learn_pytest.py目录中执行测试,同样的事情也会发生。这里我漏掉了什么?
发布于 2019-02-02 04:51:39
您的模块(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$ 发布于 2019-02-02 04:54:39
这就是它:
文件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"发布于 2021-12-30 11:58:13
将类名称Learning更改为TestLearning,将函数名称create_file更改为test_create_file,这在我的示例中有效。
https://stackoverflow.com/questions/54486852
复制相似问题