Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test skips test class if constructor is defined

Tags:

python

pytest

I have following unittest code running via py.test. Mere presence of the constructor make the entire class skip when running py.test -v -s

collected 0 items / 1 skipped

Can anyone please explain to me this behaviour of py.test?

I am interested in understanding py.test behaviour, I know the constructor is not needed.

Thanks, Zdenek

class TestClassName(object):     def __init__(self):        pass      def setup_method(self, method):        print "setup_method called"      def teardown_method(self, method):        print "teardown_method called"      def test_a(self):        print "test_a called"        assert 1 == 1      def test_b(self):        print "test_b called"        assert 1 == 1 
like image 717
Zdenek Maxa Avatar asked Jan 29 '14 12:01

Zdenek Maxa


People also ask

How do I skip Pytest?

The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark. skip(reason="no way of currently testing this") def test_the_unknown(): ...

What is a test class in Pytest?

pytest is a mature full-featured Python testing tool that helps you write better programs. Pytests may be written either as functions or as methods in classes – unlike unittest, which forces tests to be inside classes. Test classes must be named “Test*”, and test functions/methods must be named “test_*”.

What does Py test do?

Pytest is a Python testing framework that originated from the PyPy project. It can be used to write various types of software tests, including unit tests, integration tests, end-to-end tests, and functional tests. Its features include parametrized testing, fixtures, and assert re-writing.


2 Answers

The documentation for py.test says that py.test implements the following standard test discovery:

  • collection starts from the initial command line arguments which may be directories, filenames or test ids. recurse into directories, unless they match norecursedirs
  • test_*.py or *_test.py files, imported by their package name.
  • Test prefixed test classes (without an __init__ method) [<-- notice this one here]
  • test_ prefixed test functions or methods are test items

So it's not that the constructor isn't needed, py.test just ignores classes that have a constructor. There is also a guide for changing the standard test discovery.

like image 82
Matti Lyra Avatar answered Sep 19 '22 17:09

Matti Lyra


As already mentioned in the answer by Matti Lyra py.test purposely skips classes which have a constructor. The reason for this is that classes are only used for structural reasons in py.test and do not have any inherent behaviour, while when actually writing code it is the opposite and much rarer to not have an .__init__() method for a class. So in practice skipping a class with a constructor will likely be what was desired, usually it is just a class which happens to have a conflicting name.

Lastly py.test needs to instantiate the class in order to execute the tests. If the constructor takes any arguments it can't instantiate it, so again skipping is the right thing to do.

like image 40
flub Avatar answered Sep 21 '22 17:09

flub