Table of contents
The first one: Get data through yaml file (one-dimensional list)
Second type: Obtain data through yaml file (two-dimensional list)
The third type: Get data through yaml file (@pytest.fixture)
The first one: Get data through yaml file (one-dimensional list)
The content of the data.yaml file is as follows:
- 'Software Testing'
- 'Unit Testing'
- 'Automated Testing'
- 'Performance Testing'
- 'Test Development'
- 'Test Architect'
The test case content is as follows:
@pytest.mark.parametrize('data', yaml.load(open('data.yml', 'r')))
def test_ddt(data):
url='https://www.baidu.com/search/query?key='
header = {'Accept': "application/json",
'Content-Type': "application/json; charset=utf-8",
'Accept-Encoding': "gzip, deflate, br"}
res=requests.get(url+data, header)
assert res.status_code==200
Second type: Obtain data through yaml file (two-dimensional list)
The content of the data.yaml file is as follows:
#用例1
-
api_name: page_title
url: http://www.baidu.com/
header = {'Accept': "application/json",
'Content-Type': "application/json; charset=utf-8",
'Accept-Encoding': "gzip, deflate, br"}
data: {
"status_code": 200
}
#用例2
-
api_name: searching
url: http://www.baidu.com/
header = {'Accept': "application/json",
'Content-Type': "application/json; charset=utf-8",
'Accept-Encoding': "gzip, deflate, br"}
data: {
"status_code": 200
}
#用例3
-
api_name: login
url: http://www.baidu.com/
header = {'Accept': "application/json",
'Content-Type': "application/json; charset=utf-8",
'Accept-Encoding': "gzip, deflate, br"}
data: {
"status_code": 200
}
The test case content is as follows:
@pytest.mark.parametrize('data', yaml.load(open('data.yml', 'r')))
def test_ddt(data):
api_name = data['api_name']
url=data['url']
header = data['header']
res=requests.get(url + api_name, header)
assert res.status_code ==data['data']['status_code']
The third type: Get data through yaml file (@pytest.fixture)
@pytest.fixture()
def login(request):
name = request.param
print(f"== 账号是:{name} ==")
return name
data = ["pyy1", "polo"]
@pytest.mark.parametrize("login", data, indirect=True)
def test_name(login):
print(f" 测试用例的登录账号是:{login} ")
@pytest.fixture()
def logins(request):
param = request.param
print(f"账号是:{param['username']},密码是:{param['pwd']}")
return param
data = [ {"username": "name1", "pwd": "pwd1"}, {"username": "name2", "pwd": "pwd2"} ]
@pytest.mark.parametrize("logins", data, indirect=True)
def test_name_pwd(logins):
print(f"账号是:{logins['username']},密码是:{logins['pwd']}")
# 多个fixture
@pytest.fixture(scope="module")
def input_user(request):
user = request.param
print("登录账户:%s" % user)
return user
@pytest.fixture(scope="module")
def input_psw(request):
psw = request.param
print("登录密码:%s" % psw)
return psw
data = [("name1", "pwd1"), ("name2", "pwd2")]
@pytest.mark.parametrize("input_user,input_psw", data, indirect=True)
def test_more_fixture(input_user, input_psw):
print("fixture返回的内容:", input_user, input_psw)
name = ["name1", "name2"]
pwd = ["pwd1", "pwd2"]
@pytest.mark.parametrize("input_user", name, indirect=True)
@pytest.mark.parametrize("input_psw", pwd, indirect=True)
def test_more_fixture(input_user, input_psw):
print("fixture返回的内容:", input_user, input_psw)
How to obtain information
【Message 777】
Friends who want to get the source code and other tutorial materials, please like + comment + collect , three times in a row!
After three consecutive rounds , I will send you private messages one by one in the comment area~