Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test doesn't collect tests that are not inherited from 'object'

I'm trying to create Base class with different setups I need for my tests. I want all my tests to be inherited from this Base class. As runner I use py.test.

But when I'm trying to do so, py.test doesn't collect these tests inherited from Base class, and as a result it doesn't run them. Didn't find any useful info in documentation.

Maybe someone faced such issues before? Any ideas?

Thanks.

P.S. When tests are inherited from object everything works fine. Py.test see them and run correctly.

Example of code:

My Base class:

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

My Test 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'])

Output:

============================= 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]  
like image 582
Gena Olegovich Avatar asked Dec 18 '12 18:12

Gena Olegovich


1 Answers

I don't think you can use __init__. If you what to setup (or initialize) the Class 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 you can create another "method" and decorate it with @pytest.fixture as well. HOWEVER, you must remember that those "initialization methods" are called in alphabetical order.

like image 94
Alex Okrushko Avatar answered Nov 09 '22 19:11

Alex Okrushko