Reputation: 41665
I 'm writing test cases in the following manner.
# content of test_class.py
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
test_two depends on test_one, so ordering of the execution is important, What's the convention to enforce test execution order when you group tests in a class?
Upvotes: 8
Views: 7043
Reputation: 51
You can use pytest_collection_modifyitems
hook to change the order as you wish.
Upvotes: 3
Reputation: 2089
By default the tests will be executed in the order they are defined in the class/module. In your case:
test_class.py::TestClass::test_one PASSED
test_class.py::TestClass::test_two PASSED
Consider that in general it's a bad practise writing tests that are dependent on each other. If later tests are run in parallel, you will have flakiness
, or if you install a plugin for random test execution e.g. https://pypi.python.org/pypi/pytest-randomly, or if you leave the project and someone else will have to debug tests that would start failing out of the blue.
I'd recommend combining two tests into one. All that matters is you have some test scenario. Does it matter if you have 2 tests or 1 if you still have same confidence in your code?
Upvotes: 6