Share a guide on how to pass variables from the test case file to the fixture function in PyTest.

First, explain the application scenario

  • There is currently one project in the root directory of the groupconftest.pyFile, there’s a build hereapi tokenThe fixture function of thegen_token().
  • Under each case package, there will also be oneconftest.pyIs used to store the test cases applicable to this modulefixtureThe function, let’s say I have a function calledsetup_before().
  • In case, for example, there is a tokentest_case()The fixture function at the top level is passed, and that’s ittest_case(gen_token).
  • The top of thegen_token(), is required to pass three parameters. Since different cases may involve generating tokens for different users, we put this parameter in the case file.

Ok, the big picture is this.

Setup_before (); setup_before(); setup_before() So, the question can be translated into:

  • You need to pass arguments from the case file into the fixture function.
  • Gen_token (), setup_before() and test_case().

Second, use @pytest.Mark.parametrize and calls to fixtures

Here is the actual code abstract, into a simple code, easy to demonstrate and understand:

Py conftest.py test_case.py __init__.py conftest.py test_case.py __init__.py conftest.pyCopy the code

Py, /demo_top/demo_sub/conftest.py, /demo_top/demo_sub/conftest.py, / demo_TOP /demo_sub/test_case.

1. /demo_top/conftest.py

# content of /demo_top/conftest.py import pytest @pytest.fixture() def gen_token(request): If params[0] + params[1] == 5: return "api_token" else: if params[0] + params[1] == 5: return "api_token" else: return NoneCopy the code

Here, the fixture function that simulates generating the token returns “API_token” when the passed values add up to 5, or None otherwise.

2. /demo_top/demo_sub/conftest.py

# content of /demo_top/demo_sub/conftest.py import pytest @pytest.fixture() def setup_before(request, gen_token): Print (" setup_before ", "gen_token") print(" setup_before ", "gen_token") if gen_token: Yield "print(" test_case completed, clean test data ") else: pytest.skip(" skip ")Copy the code

This simulates the fixture function that builds data for the test case, and skips the test case if the token isn’t given.

3. /demo_top/demo_sub/test_case.py

# content of /demo_top/demo_sub/test_case.py import pytest test_param = [(1, 4)] @pytest.mark.parametrize("gen_token", test_param, indirect=True) @pytest.mark.parametrize("setup_before", test_param, indirect=True) def test_case1(gen_token, setup_before): Print (" setup_before: ", setup_before) print(" test_case1..." ) if __name__ == '__main__': pytest.main(['-s', 'test_case.py'])Copy the code

This is the test case file. The test function test_case1 needs to use the values returned by two fixture functions, so gen_token and setup_before are required.

Parameter passing

  • Pytest.mark. parametrize: Use pyTest’s built-in Parametrize to pass parameters to the target fixture function, adding the parameters to whichever fixture function you want. Like this one right heregen_tokenandsetup_before, note that the name is the same as the fixture name.
  • Indirect =True: the effect is the name of the parameter in make parametrize, i.e"gen_token"Execute as a function, and the following parameter valuestest_paramAs a"gen_token"The refs.
  • Request. param: the fixture function that accepts pass-through parameters, used byrequest.paramTo get the value.

Fixtures call fixtures The way fixtures call each other has been detailed in previous articles. Setup_before (request, gen_token);

Some print was done in each link to print out information to help understand the execution process.

Test_case. Py [100%] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1 passed in 0.08 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Gen_token () : setup_before; gen_token() : setup_before; gen_token() : api_token. Api_token Execute test case test_casE1... Clearing test data Process finished with exit code 0 when test case test_case is executedCopy the code

Test_param = [(2, 4)];

Test_case. Py [100%] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1 skipped in 0.08 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = s 2. To bounce lightly over: 2. To bounce lightly over: 2. To bounce lightly over: 2. To bounce lightly over: 3. Skip Process Finished with exit code 0Copy the code

The test case is not executed.