2

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 =^/

tcratius
  • 525
  • 6
  • 15

1 Answers1

3

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.
hansaplast
  • 11,007
  • 2
  • 61
  • 75
  • OK, makes sense. Unfortunately this causes another issue which was initially solve by using @classmethod. Sigh – tcratius Dec 19 '17 at 07:12
  • can you explain the issue you run into when you remove `@classmethod`? – hansaplast Dec 19 '17 at 07:26
  • Wasn't sure if I was allowed to do this on stakeoverflow, must confess I try not to post on here. NameError: global name 'parse_object' is not defined <--- parse_object is a function in the parser.py file. – tcratius Dec 19 '17 at 08:54
  • 1
    @ConradThiele I added a section in my answer. Please read up some documentation about classes in python, then you need to rework your code – hansaplast Dec 19 '17 at 15:45
  • Cheers, I have been struggling with classes for a good six months. Not the brightest tool in the shed. I look more into to classes. Thank you for helping :) – tcratius Dec 21 '17 at 14:23
  • People take it for granted being smart. Oh well, will keep trying. – tcratius Dec 21 '17 at 14:35
  • no worries, I also started Python a while back. It takes time. But learning about python basics you better read up some basic documentation first, I cannot really transfer this knowledge over stackoverflow answers – hansaplast Dec 21 '17 at 14:38
  • if you look at lexicon.py I think I achieved what you are referring to and yes getting the basics right is definitely important – tcratius Dec 22 '17 at 00:55