pytest - test flow order

女生的网名这么多〃 提交于 2019-12-13 02:27:51

问题


I have a pytest code similar to below... if i run it with --count 3 it will run the test_first 3 times then test_second 3 times.

What if i would like it to run test_first, test_second and repeat that flow?

Thanks.:)

@pytest.mark.usefixtures('setup')
class TestSomething:

    def run_setup(self):
        pass

    def test_first(self):
        print('test 1')
        name = 'name'
        assert name.isalpha()

    def test_second(self):
        print('test 2')
        name = '12345'
        assert name.isalpha()

回答1:


You can implement it yourself. Take a look at the pytest_collection_modifyitems hook where you can alter the list of tests to be executed. Example:

# conftest.py
import pytest

def pytest_addoption(parser):
    parser.addoption('--numrepeats', action='store', type=int, default=1)

def pytest_collection_modifyitems(items):
    numrepeats = pytest.config.getoption('--numrepeats')
    items.extend(items * (numrepeats - 1))

When put into a conftest.py file in the tests root dir, this code adds a new command line option numrepeats that will repeat the test run n times:

$ pytest --numrepeats 3



回答2:


Based on https://pytest-ordering.readthedocs.io (alpha) plug-in you can do:

import pytest

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

@pytest.mark.order1
def test_bar():
    assert True

See also a discussion at Test case execution order in pytest.

My personal take on this if your tests require sequence, they are not really well isolated and some other test suit design is possible.



来源:https://stackoverflow.com/questions/51539570/pytest-test-flow-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!