问题
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 there any way to split this test in two, where a second test depends on a first?
回答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:
def test_service_one(param, id):
assert check_service_one(param) == id
def test_service_two(id):
check_service_two(id) # I'm assuming this does some assertion of its own.
@pytest.fixture
def param(param_and_id):
param, _ = param_and_id
return param
@pytest.fixture
def id(param_and_id):
_, id = param_and_id
return id
@pytest.fixture(
params=[
(1, EXPECTED_ID_FOR_PARAM_1),
(2, EXPECTED_ID_FOR_PARAM_2),
(3, EXPECTED_ID_FOR_PARAM_3),
],
)
def param_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).
来源:https://stackoverflow.com/questions/37613883/py-test-passing-results-of-one-test-to-another