【发布时间】:2018-06-01 10:43:27
【问题描述】:
从 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
从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 固定装置的问题
$ 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。测试从 parser.py 中调用函数。 github.com/tcratius/ex48.git