Pytest没有测试

时间:2017-12-19 03:10:18

标签: 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,看看是否存在问题但没有成功。我也检查堆栈类似的问题,但找不到答案。我必须承认这是我学过的第一语言,所以我对这整个测试都很新。这是我的项目树 . ├── 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

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

删除@classmethod后,正确回答hansaplast,我需要更改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 = ^ /

,可能更容易删除和重新开始

1 个答案:

答案 0 :(得分:3)

快速回答:删除 window.location.href = '<?= base_url().' sijb / home '?>'; 注释,系统将调用您的测试。

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

  

删除@classmethod后,正确回答hansaplast,我需要更改test_sentence并添加test_sentence(self)

我很快查了your code on github,你的代码有几个问题

  • 为什么你需要@classmethod?您只是添加注释但不使用它
  • 您需要查找正确使用类。你的类@pytest.fixture()只有函数(即:它们都没有使用Sentence作为参数),同样,在self中你有自己作为第一个参数的函数,即使它们不是在课堂上。类外的函数不需要parser_tests.py,类中的函数(=方法)将self作为第一个参数。
相关问题