Py.test does not collect tests that are not inherited from the "object"

I am trying to create a Base class with different settings that I need for my tests. I want all my tests to be inherited from this Base class. As a runner, I use py.test .

But when I try to do this, py.test does not collect these tests inherited from the Base class, and as a result, it does not run them. Did not find useful information in the documentation.

Perhaps someone has encountered such problems before? Any ideas?

Thanks.

PS When tests are inherited from an object , everything works fine. Py.test see them and run correctly.

Code example:

My base class:

class BaseClass(object): """BaseClass""" def __init__(self): super(BaseClass, self).__init__() self.bla = 'bla' 

My testing class:

 import pytest from base_class import BaseClass class TestSmth(BaseClass): def test_test(self): test_instatnce = TestSmth() print test_instatnce.bla if __name__ == '__main__': pytest.main([__file__, '-v']) 

Conclusion:

 ============================= test session starts ============================== platform darwin -- Python 2.7.2 -- pytest-2.3.3 -- /usr/bin/python collecting ... collected 0 items =============================== in 0.01 seconds =============================== [Finished in 0.4s] 
+6
source share
1 answer

I do not think you can use __init__ . If you need to configure (or initialize) a class that you can do:

 # base_class.py class BaseClass(object): """BaseClass""" @classmethod @pytest.fixture(scope = "class", autouse = True) def setup(self): self.bla = 'bla' #test_class.py import pytest from base_class import BaseClass class TestSmth(BaseClass): def test_test(self): print self.bla if __name__ == '__main__': pytest.main([__file__, '-v']) 

Also, to initialize the TestSmth class TestSmth you can create another โ€œmethodโ€ and decorate it with @pytest.fixture . HOWEVER, you must remember that these "initialization methods" are called in alphabetical order .

+5
source

All Articles