Search code examples
pythonintegration-testingpytest

how to write integration tests using pytest and how to repeat the integration tests


I am new to this so please do not mind if the question is not specific enough.

I want to know how to club unit tests into a single integration test in pytest. Furthermore, I would like to repeat the integration test in a single test session a couple of times. Please let me know if there is a way to do this in pytest.

Scenario: I have two unit tests name test_start_call and test_end_call that are invoked by pytest in that order.

Now I wanted to repeat the process a couple of times so I did this:

for i in range(0,c):
      pytest.main(some command)

which works fine which will start the test session and tear down the test session as many times as I want with one call being made in each test session.

But I want to make several calls in a single test session and by far I have not found any way to do this since last two days. I tried looking into xdist but I don't want to start new processes in parallel. The integration tests should serially execute unit tests (start call and end call) as many times as I want in a single test session.

I am stuck. So any help would be great. Thank you!


Solution

  • review https://docs.pytest.org/en/latest/parametrize.html

    Then add mult marker to each test and consume it in hook pytest_generate_tests to provide multiple tests fixture value will be visible in --collect-only --mult 3. Using marker this way will constrain the multiple tests mechanism to only marked tests.

    # conftest
    def pytest_addoptions(parser):
        parser.addoption('--mult', default=0, help="run many tests")
    
    def pytest_generate_tests(metafunc):
        count = int(metafunc.config.getoption('--mult'))
        if count and metafunc.get_closest_marker('mult'):
            if 'mult' not in metafunc.fixturenames:
                metafunc.fixturenames.append('mult') 
            metafunc.parametrize("mult", range(count))
    
    # testfoo
    @pytest.mark.mult
    def test_start_call():
        ...