Reputation: 397
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!
Upvotes: 8
Views: 10742
Reputation: 512
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():
...
Upvotes: 3
Reputation: 183
From what you're saying, I'm not quite sure that you are using the right toolset. It sounds like you are either trying to load test something ( run it multiple times and see if it falls over ), or trying to do something more "data driven" - aka given input values x through y, see how it behaves.
If you are trying to do something like load testing, I'd suggest looking into something like locust.
Here is a reasonable blog with different examples on driving unit tests via different data.
Again, not sure if either of these are actually what you're looking for.
Upvotes: 2