Python+Appium+Pytest automated testing - parameterized settings

Notes from beginners of automated testing on the Android side of the APP, please advise me if I am wrong. (All content is based on Weibo V10.11.2 version as an example)

During the execution of automated test cases, the same use case is often executed, but different parameters are passed in, which leads to the need to write test cases repeatedly, which will make our use cases very long, redundant, and require coding in many places. Parameterization can modify the parameters passed in, so that the use cases of the same steps can be executed multiple times, and the test cases can be called iteratively.

Parameterized settings

One: Use decorators to achieve parameterized settings

Parameterization requires the use of pytest decorators: @pytest.mark.parametrize()

方法:parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)

 

In the above example, all content has been logged in with Weibo account and password as an example

1. Pass in a parameter, a parameter corresponds to a value

import pytest
 
classTestAccountLogin:
 
    # 参数化:传入一个参数,一个参数对应一个值    @pytest.mark.parametrize("account", ["123123231321313"])deftest_one(self, account):
        pwd = "asdfgh"
        self.account_login_page.input_account_pwd(account, pwd)
        print("\na的值:", account)

 

The result of the operation is:

2. Pass in two parameters, one parameter corresponds to a value

import pytest
 
classTestAccountLogin:
 
    # 参数化:传入两个参数,一个参数对应一个值    @pytest.mark.parametrize("account, pwd", [("123123231321313", "asfgh")])deftest_one(self, account, pwd):
        self.account_login_page.input_account_pwd(account, pwd)
	print("\naccount的值:", account, "\npwd的值:", pwd)

The result of the operation is:

3. Pass in two parameters, one parameter corresponds to multiple values

import pytest
 
classTestAccountLogin:
 
    # 参数化:传入两个参数,一个参数对应两个值    @pytest.mark.parametrize("account, pwd", [
        ("123123231321313", "asdfgh"),
        ("12345645612", "123123")
    ])deftest_one(self, account, pwd):
        self.account_login_page.input_account_pwd(account, pwd)
	print("\naccount的值:", account, "\npwd的值:", pwd)

 

The result of the operation is;

Note: The first parameter of the @pytest.mark.parametrize() decorator is in the form of a string to represent the parameters of the use case function, the second parameter passes the test data in the form of a list or tuple, and the parameters of the decorator are the same as The parameters passed into the use case function are consistent.

4. To get all combinations of multiple parameterized parameters, you can stack parametrize decorators

import pytest
 
classTestAccountLogin:
    # 所有参数的组合    @pytest.mark.parametrize("account", ["123123123123", "1456456456456", "1789789789789"])    @pytest.mark.parametrize("pwd", ["we", "you", "he"])deftest_one(self, account, pwd):
        self.account_login_page.input_account_pwd(account, pwd)
        print("\naccount的值:", account, "\npwd的值:", pwd)

 

The result of the operation is:

Note: As shown in the figure above, it can be seen that the combination of all parameters is to combine and match the data of parameter 1 with all the data of parameter 2

Two: Parameterized reading of internal list data

Create a data list in the test class to store the data corresponding to the parameters. This method writes the parameter data involved in each test class inside the class, and can quickly and conveniently modify the parameter data at runtime.

import pytest
 
# 建立数据列表,存放传入参数对应的数据
data = [("w124hhh77", "111"),
        ("q123457gg", "222"),
        ("rdde54sds", "333")
       ]
 
classTestAccountLogin:
    # 参数化数据读取内部列表数据    @pytest.mark.parametrize("account, pwd", data)deftest_one(self, account, pwd):
        self.account_login_page.input_account_pwd(account, pwd)
        assert self.account_login_page.get_bounced_context() == "你尚未注册微博,是否立即注册"

The result of the operation is:

Three: Parameterized reading from external yaml files
Using parameterized reading of external yaml files, only the data files need to be maintained, and new test case data can be dynamically added without changing the data in the code.

To read the external yaml file, you need to install the yaml package first, enter the command line: pip install pyyaml, after the installation is successful, as shown in the figure below. To install in PyCharm, go to File→setting, search for pytest intrepreter, click the "+" sign, search for PyYAML, and install it.

First create a .yaml file under the project directory (that is, outside the test class)

 

Set the corresponding value of the incoming parameter in the yaml file

Implement parameterized reading of external yaml files in the test class

import pytest
import yaml
 
classTestAccountLogin:
    # 在初始化前面先获取yaml文件
    account_data = yaml.safe_load(open("E:\\study\\Fork\\WeiboDemo\\Weibo\\data\\account_login.yaml", "r"))
    print(account_data)
 
    # 参数化数据读取外部文件yaml    @pytest.mark.parametrize("account, pwd", account_data)deftest_two(self, account, pwd):
        self.account_login_page.input_account_pwd(account, pwd)
        assert self.account_login_page.get_bounced_context() == "你尚未注册微博,是否立即注册"

 

The result of the operation is:

Here, when using a relative path when opening the yaml file, there will be an error of No such file or directory: 'account_login.yaml'. You need to use an absolute path. I don't know why. If you are free, can you help me out? Thanks! 

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/okcross0/article/details/130069034