Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest ordering from different test case files

Tags:

python

pytest

Hi I use pytest and have following 2 py files in a folder.

test_abc.py as below:

class MyTest(unittest.TestCase):
    @classmethod
    def setup_class(cls):
        cls.a = 10

    @classmethod
    def teardown_class(cls):
        cls.a = 20

    @pytest.mark.run(order=2)
    def test_method1(self):
       logging.warning('order2 in test_abc')
       assert (10,self.a)    # fail for demo purposes

    @pytest.mark.run(order=1)
    def test_method2(self):
        logging.warning('order1 in test_abc')
        assert 0, self.db   # fail for demo purposes

test_sample2.py as below,

class MyTest1(unittest.TestCase):
    @classmethod
    def setup_class(cls):
        cls.a = 10

    @classmethod
    def teardown_class(cls):
        cls.a = 20

    @pytest.mark.run(order=2)
    def test_mtd1(self):
       logging.warning('order2 in test_samp')
       assert (10,self.a)    # fail for demo purposes

    @pytest.mark.run(order=1)
    def test_mtd2(self):
        logging.warning('order1 in test_samp')
        assert 0, self.db   # fail for demo purposes

Now I run using command:

py.test --tb=long --junit-xml=results.xml --html=results.html -vv

What happens here is test_method2 from both the test case files runs first (since it has been given as order1) and then test_method1 runs from both files (since it has been give as order 2)

So What I have noticed here is Ordering is overall for the test run and not for individual class/files

Is there any way to fix this issue? Right now I use ordering number for all like first file i give (1,2) then in next file i give (3,4) and it works fine.

But I don't wan't ordering in all test class only in few places I require it. Is there any hook to say pytest to see ordering only in particular file?

like image 533
Naggappan Ramukannan Avatar asked Nov 16 '25 08:11

Naggappan Ramukannan


1 Answers

I assume you're using the pytest-ordering plugin -- if only particular areas in your tests require ordering, you could use relative ordering:

@pytest.mark.run(after='test_second')
def test_third():
    assert True

def test_second():
    assert True

@pytest.mark.run(before='test_second')
def test_first():
    assert True

Reference: (http://pytest-ordering.readthedocs.org/en/develop/#relative-to-other-tests)

like image 62
Adam Avatar answered Nov 18 '25 20:11

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!