首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pytest不使用pytest命令收集测试

Pytest不使用pytest命令收集测试
EN

Stack Overflow用户
提问于 2019-02-02 04:38:38
回答 4查看 3.2K关注 0票数 0

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

代码语言:javascript
复制
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目录中执行测试,同样的事情也会发生。这里我漏掉了什么?

EN

回答 4

Stack Overflow用户

发布于 2019-02-02 04:51:39

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

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

代码语言:javascript
复制
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$ 
票数 1
EN

Stack Overflow用户

发布于 2019-02-02 04:54:39

这就是它:

文件test_learn_pytest.py

代码语言:javascript
复制
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"
票数 0
EN

Stack Overflow用户

发布于 2021-12-30 11:58:13

将类名称Learning更改为TestLearning,将函数名称create_file更改为test_create_file,这在我的示例中有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54486852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档