Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest: run test one by one

Tags:

python

pytest

I've multiple test functions created in a file. Example:

def testA():
   change_user_permission_to_allow()
   assert action == success
   change_user_permission_to_deny()

def testB():
   assert action == fail

## Multiple other tests...

By default, user are denied for action. When I run testB individually, it passes. When I run the test file as a whole.

pytest testfile.py

The testB fails. When I debug the user permission is allowed. Seems like testA is making issue in testB. Is there a way to tell pytest to run test one after another?

like image 645
PaxPrz Avatar asked Feb 16 '26 14:02

PaxPrz


1 Answers

  • you can read the pytest-ordering: run your tests in order
  • With pytest-ordering, you can change the default ordering as follows:
import pytest

@pytest.mark.order2
def test_foo():
    assert True

@pytest.mark.order1
def test_bar():
    assert True
$ py.test test_foo.py -vv
============================= test session starts ==============================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- env/bin/python
plugins: ordering
collected 2 items

test_foo.py:7: test_bar PASSED
test_foo.py:3: test_foo PASSED

=========================== 2 passed in 0.01 seconds ===========================
like image 137
KennetsuR Avatar answered Feb 18 '26 04:02

KennetsuR



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!