Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest - test case execution order

Tags:

python

pytest

I have some scripts with the tests for them and I need to run these tests in execution order explicitly defined by me.

It looks like:

# one.py
import some lib

class Foo():
    def makesmth(self)
        script

then I have made test files:

# test_one.py
import pytest
import some lib

class TestFoo():
    def test_makesmth(self):
        try/except/else assert etc.

So it looks simple and right. When I run file test_one.py everything is ok. Package of my scripts-testing looks like:

package/
|-- __init__.py
|-- scripts
|   |-- one.py
|   |-- two.py
|-- tests
|   |-- test_one.py
|   |-- test_two.py

When I try to collect test with

pytest --collect-only

it is giving non-alphabetical and just randomly order of tests.

Where I can write information about order of tests? Non-alphabetical, just like I want to start test like b, a, c, e, d - and not random not alphabetical

Tried to made file tests.py:

import pytest

from tests.test_one import TestFoo
from tests.test_two import TestBoo etc.

And when I'm trying to run this, errors are shown, because these imports were done in the way I don't understand (tried to make aTestFoo bTestBoo and also rename test files in that method definition way but still it's doesn't work).

like image 263
alla Avatar asked Dec 21 '18 15:12

alla


1 Answers

Pytest-ordering seems abandoned at the moment, you can also check out pytest-order (a fork of the original project).

import pytest

@pytest.mark.order(2)
def test_foo():
    assert True

@pytest.mark.order(1)
def test_bar():
    assert True
like image 159
Roelant Avatar answered Oct 04 '22 00:10

Roelant