Tips - How to avoid pytest test case random execution order
I have written multiple test case modules over the period using pytest but then comes a scenario where I was forced to execute the test case in the same order as I wrote in the test script.
All this days I have never worried about the test case execution order but when the problem arose I checked for existing options available in command line but no luck. So as usual my next mantra is to search for answers in google.
But things got really tricky this time, though after spending for couple of minutes I ended up with approved answers from stackoverflow but it's of no use. Yes !! It doesn't worked for me ( Poor me :'( ).
Only when I started to explore more found that the older approved answers in stackoverflow still got some works to do in it (Approved answers don't work if we have 'random' plugin enabled in the fixtures as part of test case setup). So I started to dig deeper and later found a working command line options (Eureka mode ! :D ). So why not share the answers if you know it works and hence I started to end up here to share the answer as a tip to all my dear followers .
Tips - Pytest test case execution order |
To avoid random test case execution order in pytest, all you need to do is to simply add one of the below command line options available in pytest,
- --randomly-dont-reorganize
- -p no:randomly
Module:
import pytest
def test_three():
assert True
def test_four():
assert True
def test_two():
assert True
def test_one():
assert True
Execution:
(tmp.w95BqE188N) rkalaiselvan@dev-rkalaiselvan:~/$ py.test --randomly-dont-reorganize test_dumm.py
======================================================================== test session starts ========================================================================
platform linux2 -- Python 2.7.12, pytest-3.10.1, py-1.5.4, pluggy-0.7.1 -- /tmp/tmp.w95BqE188N/bin/python2
cachedir: .pytest_cache
Using --randomly-seed=1566829391
rootdir: /home/rkalaiselvan, inifile: pytest.ini
plugins: randomly-1.2.3, timeout-1.3.1, cov-2.6.0, mock-1.10.0, ordering-0.6
collected 4 items
test_dumm.py::test_three PASSED
test_dumm.py::test_four PASSED
test_dumm.py::test_two PASSED
test_dumm.py::test_one PASSED
(tmp.w95BqE188N) rkalaiselvan@dev-rkalaiselvan:~/$ py.test -p no:randomly test_dumm.py
======================================================================== test session starts ========================================================================
platform linux2 -- Python 2.7.12, pytest-3.10.1, py-1.5.4, pluggy-0.7.1 -- /tmp/tmp.w95BqE188N/bin/python2
cachedir: .pytest_cache
Using --randomly-seed=1566829391
rootdir: /home/rkalaiselvan, inifile: pytest.ini
plugins: randomly-1.2.3, timeout-1.3.1, cov-2.6.0, mock-1.10.0, ordering-0.6
collected 4 items
test_dumm.py::test_three PASSED
test_dumm.py::test_four PASSED
test_dumm.py::test_two PASSED
test_dumm.py::test_one PASSED
Reference:
I have even written an working answer in the same stackoverflow question I searched for,
Feel free to drop in your feedback in the comment section below, will see you all again in next blog post. Cheers.
No comments