Skip to content Skip to sidebar Skip to footer

Py.test Passing Results Of One Test To Another

Currently I have test looking like this: @pytest.mark.parametrize('param', [1,2,3]) def test_two_services(param): id = check_service_one(param) check_service_two(id) Is th

Solution 1:

Remember to test at the boundary. So if the the values of id depend solely on param and if id isn't some implementation detail, but a part of the defined behaviour of the system under test, split up your tests like so:

deftest_service_one(param, id):
    assert check_service_one(param) == iddeftest_service_two(id):
    check_service_two(id)  # I'm assuming this does some assertion of its own.@pytest.fixturedefparam(param_and_id):
    param, _ = param_and_id
    return param

@pytest.fixturedefid(param_and_id):
    _, id = param_and_id
    returnid@pytest.fixture(
    params=[
        (1, EXPECTED_ID_FOR_PARAM_1),
        (2, EXPECTED_ID_FOR_PARAM_2),
        (3, EXPECTED_ID_FOR_PARAM_3),
    ],
)defparam_and_id(request):
    return request.param

Like this, the tests are loosely coupled by the inputs of check_service_two matching the expected (and checked by assertion in test_service_one) results of check_service_one, rather than test_service_two depending hard on test_service_one. Thus, the tests can be run in arbitrary order and any one test can be run isolated (without having to run another test first).

Post a Comment for "Py.test Passing Results Of One Test To Another"