python - Pytest 未运行任何测试

标签 python bash pytest

在 Linux mint bash 中我输入

$ pytest tests/parser_test.py 
=============================== test session starts py ================================
platform linux2 -- Python 2.7.14, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: /home/rat/Projects/ex48, inifile:
collected 0 items                                                                   

=========================== no tests ran in 0.00 seconds ===========================

但它说“没有测试运行”。我分阶段进行了每个功能测试,并且工作了一段时间,但我注意到,每个附加功能 pytest 都会获得更少的测试。我有一种有趣的感觉,这是我的测试代码,但是我不确定我做错了什么,甚至不知道要搜索什么。我尝试将一些变量从 word_list_one 更改为 wl_one,看看这是否是问题所在,但没有成功。我还检查了类似问题的堆栈,但找不到答案。我必须承认这是我学到的第一门语言,所以我对整个测试的事情非常陌生。这是我的项目树 。 ├── 前48 │   ├── __init__.py │   ├── __init__.pyc │   ├── 词典.py │   ├── 解析器.py │   └── 解析器.pyc ├── 自述文件.md ├── setup.py └── 测试 ├── __init__.py ├── __init__.pyc ├── lexicon_tests.py ├── parser_test.py └── __pycache__ ├── parser_test.cpython-27-PYTEST.pyc └── parser_tests.cpython-27-PYTEST.pyc

import pytest
from ex48 import parser
from ex48.parser import Sentence

# test parse_sentence
@pytest.fixture()
def test_sentence():
    sentence = Sentence.parse_sentence([("noun","player"),("verb", "go"),("noun", "bear")])
    return sentence


def test_peek():
    result = test_sentence()
    assert Sentence.peek(result) != None

从 hansaplast 中删除 @classmethod、正确答案后,我需要更改 test_sentence 并将 test_sentence(self) 添加到 ``` as1 per this answer :

@pytest.fixture()
def test_sentence(self):
    sentence = Sentence()
    return sentence.parse_sentence([("noun","player"),("verb", "go"),("noun", "bear")])

但是现在我的 pytest fixture 有问题

$ pytest tests/parser_test.py 
============================== ERRORS ====================================
___________________ ERROR at setup of test_peek __________________________
file /home/rat/Projects/ex48/tests/parser_test.py, line 13
    def test_peek(test_sentence):
file /home/rat/Projects/ex48/tests/parser_test.py, line 7
    @pytest.fixture()
    def test_sentence(self):
E       fixture 'self' not found
>       available fixtures: cache, capfd, capsys, doctest_namespace,
        monkeypatch, pytestconfig, record_xml_property, recwarn, 
        test_sentence, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

所以我输入了 $ pytest --fixtures tests/parser_test.py 并收到了很多对我来说没有意义的信息:

------------- fixtures defined from tests.parser_test -------------------
test_sentence
tests/parser_test.py:8: no docstring available

如果没有@pytest.fixtures,删除和重新开始可能会更容易=^/

最佳答案

快速回答:删除@classmethod注释,您的测试将被调用。

更长的答案:在类之外使用 @classmethod 是一件深奥的事情,this answer解释说它只是生成一个描述符,稍后可以使用 MyClass.attribute_name = test_peek 绑定(bind)到一个类。这意味着 pytest 不会将 test_peek 视为函数,而是将其视为对象,因此不会调用它。查看 github 上的 ex48 代码,您没有将此对象分配给任何类,因此您可能误解了 @classmethod 的含义,应该删除此注释。

After removing @classmethod, correct answer from hansaplast, I needed to change the test_sentence and added test_sentence(self)

我赶紧查了一下your code on github ,您的代码有几个问题

  • 为什么你需要@pytest.fixture()?您只是添加注释,但没有使用它
  • 您需要查找才能正确使用类。你的类 Sentence 中只有函数(即:它们都不使用 self 作为参数),而且,在 parser_tests.py 中你有函数以 self 作为第一个参数,即使它们不在一个类中。类外部的函数不采用 self,类内的函数(=方法)采用 self 作为第一个参数。

关于python - Pytest 未运行任何测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47879490/

相关文章:

python - 为什么 os.path.getsize() 在 python 中抛出错误?

python - Capistrano "do nohup python"有效, "do nohup ruby"无效

python - 如果没有返回异常,则传递异常并引发错误的方法

bash - 当 shell 脚本被杀死时终止正在运行的命令

python - 自定义 junitxml Pytest 报告以从自定义标记添加多个属性

python - 如果 SFTP 服务器触发在上传时自动移动文件,Paramiko put 方法会抛出 "[Errno 2] File not found"

linux - 从文件中删除连续包含 4 个或更多相同字符的行

linux - 使用另一个 bash 脚本暴力破解 bash 脚本

python - 覆盖pytest中的子 fixture

pytest - Airflow - DAG 完整性测试 - sqlalchemy.exc.OperationalError : (sqlite3. OperationalError)没有这样的表:变量