! [Python test automation (3) : build pytest parametric test cases] (https://p1-tt.byteimg.com/origin/pgc-image/e80728b95bc6464c9952757ed31a1908?from=pc)

This article is excerpted from the hogwarts Test House textbook!

After sharing most of pyTest’s utility features in previous articles, we’ll look at building PyTest parameterized use cases.

If the inputs and outputs to be tested are a set of data, you can organize the test data and call the same test method with different test data. Parameterization, as the name suggests, is to write different parameters into a set, and then the program automatically evaluates the use cases until the set is empty. Pytest can be parameterized with @PyTest.mark.parametrize.

Parameterize using parametrize

Parametrize () method: youll need to see action action.

def parametrize(self,argnames, argvalues, indirect=False, ids=None, \    scope=None):
Copy the code
  • Main Parameters
  • Argsnames: parameter name. It is a string, represented as multiple parameter names if separated by commas
  • Argsvalues: Argument values, a list of arguments that generate use cases for each element in the list
  • Method of use
  • Decorate the test method with @pytest.mark.Paramtrize ()
  • Parametrize (‘data’, param) ‘data’ is the custom parameter name and param is the list of imported parameters
  • Pass the custom parameter name data as a parameter to the test case test_func
  • You can then use the parameters of Data inside the test case

Create a test case, pass three groups of parameters, each group of two elements, determine whether the expression and value in each group of parameters are equal, the code is as follows:

@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+5",7),("7*5",30)])def test_eval(test_input,expected): # eval evaluates the string STR as a valid expression and returns assert eval(test_input) == expectedCopy the code

Running results:

plugins: Html-2.0.1, RERunfailures -8.0, xdist-1.31.0, ordering-0.6, \forked-1.1.3, allure-pytest-2.8.11, Metadata - 1.8.0 comes with collecting... collected 3 itemstest_mark_paramize.py::test_eval[3+5-8] test_mark_paramize.py::test_eval[2+5-7] Test_mark_paramize. Py: : test_eval [7 * 5 to 35] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 3 passed in 0.02 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

Pytest fetches three sets of data from the parameter list [(“3+5”,8),(“2+5”,7), and (“7*5”,30)], generates a test case for each set of data, and assigns two elements of each set of data to the method to be used by the test case as parameters of the test method.

Use parametrize multiple times

Multiple @PyTest.Mark.Parametrize decorators can be added to the same test case at the same time, and all the elements of these parametrize decorators are combined (like cartesian products) to produce a large number of test cases.

Scenario: For example, in the login scenario, there are n user names and M passwords to be entered. If you want to verify the user name and password, n* M combinations of test cases will be involved. If you list the data one by one, the workload will be very heavy. Pytest provides a parameterized way to automatically combine multiple sets of test data to generate a large number of test cases. Example code is as follows:

@pytest.mark.parametrize("x",[1,2])@pytest.mark.parametrize("y",[8,10,11])def test_foo(x,y): print(f) {x} , y:{y}")Copy the code

Running results:

plugins: Html-2.0.1, RERunfailures -8.0, xdist-1.31.0, ordering-0.6,\ forked-1.1.3, allure-pytest-2.8.11, Metadata - 1.8.0 comes with collecting... collected 6 itemstest_mark_paramize.py::test_foo[8-1] test_mark_paramize.py::test_foo[8-2] test_mark_paramize.py::test_foo[10-1] test_mark_paramize.py::test_foo[10-2] test_mark_paramize.py::test_foo[11-1] test_mark_paramize.py::test_foo[11-2]Copy the code

Considering the results of the above run, the test method test_foo() adds two @PyTest.mark.parametrize () decorators, each providing a list of two parameter values. Combining 2 * 3 = 6, PyTest generates six test cases. This method is usually used in the test is a complete combination of all variables, all values, can achieve a comprehensive test.

@Pytest. fixture is combined with @Pytest.Mark.parametrize

This is parameterized by combining @Pytest. fixture with @Pytest.Mark.Parametrize.

If the test data needs to be used in the fixture method as well as in the test case, you can add the parameter indirect=True with the parametrize parameter, and PyTest can implement passing the parameter into the fixture method. It can also be used in the current test case.

Parametrize source code:

    def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):
Copy the code

With the indirect parameter set to True, PyTest executes argNames as a function and passes argValues as an argument to the argNames function. Create the “test_param.py” file as follows:

Fixture (scope="module")def login_r(request): test_user_data = ['Tome', 'Jerry']@pytest.fixture(scope="module")def login_r(request): User = request.param print(f"\n {user}") return [email protected]("login_r", test_user_data,indirect=True)def test_login(login_r): A = login_r print(f" Return value of login in the test case; {a}") assert a ! = ""Copy the code

Running results:

plugins: Html-2.0.1, RERunfailures -8.0, xdist-1.31.0, ordering-0.6,\ forked-1.1.3, allure-pytest-2.8.11, Metadata - 1.8.0 comes with collecting... collected 2 itemstest_mark_paramize.py::test_login[Tome] test_mark_paramize.py::test_login[Jerry] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.02 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Process finished with exit code 0 login user: Tome PASSED [50%] the return value of login in the test case; Tome login user: Jerry PASSED [100%] The return value of login in the test case; JerryCopy the code

As you can see from the above results, login_r is passed in as a parameter to the login_r method when indirect=True, and test_user_data is passed in as a parameter to generate multiple test cases. Call login_r to retrieve the data returned by the login_r method.

Click to collect: Automation + side open + performance + RESUME + interview core tutorial materials