There is a delay in updating articles on this site, if you want to see articles about Python + Appium, please follow me over to Testhome. Testerhome.com/topics/2780…
From APP Android end automation test beginner’s notes, write wrong place we a lot of advice oh. (All content is based on Weibo V10.11.2 as an example)
In the process of automated test case execution, the same use case is often executed, but different parameters are passed in, leading to the need to write the use case repeatedly, which will make our use case become very long, redundant, and many places need to be coded. Parameterization can make iterative calls to test cases by modifying the parameters passed in so that use cases of the same step can be executed multiple times.
Parameterized setting
One: use decorators to achieve parameterization
Parameterizing requires a decorator using PyTest: @pytest.mark.parametrize()
Method: Parametrize (argnames, argvalues, indirect=False, ids=None, scope=None)
On chestnut, all content has been microblog account password login as an example
1. Pass in a parameter. Each parameter corresponds to a value
import pytest
class TestAccountLogin:
Parameterization: Pass in a parameter, each parameter corresponds to a value
@pytest.mark.parametrize("account"["123123231321313"])
def test_one(self, account) :
pwd = "asdfgh"
self.account_login_page.input_account_pwd(account, pwd)
print("\ na values.", account)
Copy the code
The running results are as follows:
2. Pass in two parameters, each corresponding to a value
import pytest
class TestAccountLogin:
Parameterization: Pass in two arguments, one for each value
@pytest.mark.parametrize("account, pwd", [("123123231321313"."asfgh")])
def test_one(self, account, pwd) :
self.account_login_page.input_account_pwd(account, pwd)
print("\ naccount value.", account, "\ NPWD:", pwd)
Copy the code
The running results are as follows:
3. Pass in two parameters. One parameter corresponds to multiple values
import pytest
class TestAccountLogin:
# parameterize: Pass in two arguments, one for two values
@pytest.mark.parametrize("account, pwd", [("123123231321313"."asdfgh"),
("12345645612"."123123")])
def test_one(self, account, pwd) :
self.account_login_page.input_account_pwd(account, pwd)
print("\ naccount value.", account, "\ NPWD:", pwd)
Copy the code
The running result is;
Note: the first argument to the @pytest.mark.parametrize() decorator is a string representing the arguments to the use case function, the second argument is passing test data as a list or tuple, and the decorator arguments are the same as those passed into the use case function.
4. To get all the combinations of parameterized parameters, stack the Parametrize decorator
import pytest
class TestAccountLogin:
A combination of all parameters
@pytest.mark.parametrize("account"["123123123123"."1456456456456"."1789789789789"])
@pytest.mark.parametrize("pwd"["we"."you"."he"])
def test_one(self, account, pwd) :
self.account_login_page.input_account_pwd(account, pwd)
print("\ naccount value.", account, "\ NPWD:", pwd)
Copy the code
The running results are as follows:
Note: As shown in the figure above, the combination of all parameters is to pair the data of parameter 1 with all the data of parameter 2
Two: parameterized read internal list data
Create a data list in the test class to store the data corresponding to the parameters. In this method, the parameter data involved in each test class is written inside the class, and the parameter data can be quickly and conveniently modified at run time.
import pytest
Create a list of values for the parameters passed in
data = [("w124hhh77"."111"),
("q123457gg"."222"),
("rdde54sds"."333")]class TestAccountLogin:
Parameterized data reads internal list data
@pytest.mark.parametrize("account, pwd", data)
def test_one(self, account, pwd) :
self.account_login_page.input_account_pwd(account, pwd)
assert self.account_login_page.get_bounced_context() == "You have not registered on Weibo, do you want to register now?"
Copy the code
The running results are as follows:
Three: Parameterized reads from external YAML files
Using parameterization to read external YAML files, you can dynamically add new test case data by maintaining the data file without changing the data in the code.
To read external YAMl files, you need to install the yaml package first. Enter PIP install Pyyaml on the command line. The following figure shows the successful installation. To install PyCharm, go to File→ Setting, search pyTest Intrepreter, click “+”, search PyYAML, and install.
Start by creating a. Yaml file in the project directory (that is, outside the test class)
Set the values of the passed parameters in the YAML file
The test class implements parameterized reading of external YAML files
import pytest
import yaml
class TestAccountLogin:
Get the YAML file before initialization
account_data = yaml.safe_load(open("E:\\study\\Fork\\WeiboDemo\\Weibo\\data\\account_login.yaml"."r"))
print(account_data)
Parameterized data is read from external file YAML
@pytest.mark.parametrize("account, pwd", account_data)
def test_two(self, account, pwd) :
self.account_login_page.input_account_pwd(account, pwd)
assert self.account_login_page.get_bounced_context() == "You have not registered on Weibo, do you want to register now?"
Copy the code
The running results are as follows:
Error: No such file or directory: ‘account_login.yaml ‘error: No such file or directory: ‘account_login.yaml’ Thanks!!