【问题标题】:Pytest no tests ranPytest 没有运行测试
【发布时间】: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

标签: python bash pytest


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 好的,有道理。不幸的是,这导致了另一个问题,该问题最初是通过使用@classmethod 解决的。叹息
  • 你能解释一下你删除@classmethod时遇到的问题吗?
  • 不确定我是否被允许在stakeoverflow上这样做,必须承认我尽量不在这里发帖。 NameError: global name 'parse_object' is not defined
  • @ConradThiele 我在答案中添加了一个部分。请阅读一些关于 python 中的类的文档,然后你需要修改你的代码
  • 干杯,我已经在课堂上苦苦挣扎了六个月。不是棚子里最亮的工具。我更关注课堂。谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 2020-04-08
  • 1970-01-01
  • 2020-04-25
  • 2020-06-14
  • 2016-07-27
  • 2023-01-25
相关资源
最近更新 更多