tcratius
tcratius

Reputation: 585

Pytest no tests ran

From Linux mint bash I type

$ 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 ===========================

and yet it says there are "no test ran". I did each function test in stages and it worked for a while but I noticed that with each additional function pytest was picking up less test(s). I have a funny feeling it is with my testing code, however I am not sure what it is that I am doing wrong or even what to search for it. I tried changing some of the variables from word_list_one to wl_one to see if that was the problem with no success. I also check stack over similar questions but could not find the answer. I must admit this is my first language that I have learnt so I'm very new to this whole testing thing. Here is my Project tree . ├── 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

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

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

However now I have an issue with the 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.

So I typed $ pytest --fixtures tests/parser_test.py and received a lot of information that doesn't make sense to me and this:

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

Might be easier to delete and start over without @pytest.fixtures =^/

Upvotes: 2

Views: 4020

Answers (1)

hansaplast
hansaplast

Reputation: 11593

Quick answer: Remove the @classmethod annotation and your test will be called.

Longer answer: Having @classmethod outside a class is a somehow esoteric thing, this answer explains that it just produces a descriptor which can later be bound to a class using MyClass.attribute_name = test_peek. That means that pytest will see test_peek not as a function but as an object and thus will not call it. Looking through your ex48 code on github you're not assigning this object to any class, so you probably misunderstood the meaning of @classmethod and should just remove this annotation.

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

I quickly looked up your code on github, there are several things wrong with your code

  • why do you need the @pytest.fixture() at all? You are just adding the annotations but not using it then
  • you need to look up to correctly work with classes. your class Sentence in only have functions (that is: none of them uses self as argument), also, in parser_tests.py you have functions which have self as first parameter, even they are not in a class. Functions outside classes don't take self, functions (=methods) within classes take self as first parameter.

Upvotes: 3

Related Questions