Pytest未运行测试

2024-06-09 05:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

从Linux mint bash I类型

$ 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,看看这是否是没有成功的问题。我也检查了堆栈相似的问题,但找不到答案。我必须承认这是我所学的第一门语言,所以我对这门考试很陌生。这是我的项目树 . ├── ex48 │   ├── __init__.py │   ├── __init__.pyc │   ├── lexicon.py │   ├── parser.py │   └── parser.pyc ├── README.md ├── setup.py └── tests ├── __init__.py ├── __init__.pyc ├── lexicon_tests.py ├── parser_test.py └── __pycache__ ├── parser_test.cpython-27-PYTEST.pyc └── parser_tests.cpython-27-PYTEST.pyc

^{pr2}$

从hansaplast中删除@classmethod,correct answer后,我需要更改测试句子,并将测试句子(self)添加到```as1 per this answer

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

但是现在我对pytest设备有一个问题

$ 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_测试.py收到了很多对我来说毫无意义的信息:

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

可能更容易删除和重新开始没有@pytest.固定装置=^/


Tags: pytestselfparserhomeinitpytesttests
1条回答
网友
1楼 · 发布于 2024-06-09 05:32:51

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

更详细的回答是:在类之外有@classmethod是一件很深奥的事情,this answer解释说它只是生成一个描述符,稍后可以使用MyClass.attribute_name = test_peek绑定到一个类。这意味着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作为第一个参数。在

相关问题 更多 >