一、参数化parametrize 1、参数化可以组装测试数据,在测试前定义好测试数据,并在测试用例中使用 #!/usr/bin/env python # -*- coding: UTF-8 -*- """ @Project :Pytest @File :test_parametrize_01.py @IDE :
一、参数化parametrize
1、参数化可以组装测试数据,在测试前定义好测试数据,并在测试用例中使用
#!/usr/bin/env python# -*- coding: UTF-8 -*-
"""
@Project :Pytest
@File :test_parametrize_01.py
@IDE :PyCharm
@Author :zhou
@Date :2022/8/6 17:37
"""
import pytest
"""
# 单次循环
@pytest.mark.parametrize("a",["b","c"])
其中a相当于key,["b","c"]相当于参数值
"""
# 单次循环,但参数值为单个时
# @pytest.mark.parametrize("name", ["zhou"])
# def test_parametrize_01(name):
# print("获取" +name)
# assert name == "zhou"
# 多次循环,当参数值为多个时,会依次赋值给key
# @pytest.mark.parametrize("name", ["zhou1", "zhou2"])
# def test_parametrize_01(name):
# print("获取" + name)
# assert name == "zhou"
"""
@pytest.mark.parametrize("key1,key2",[("a","b"),("e","f")])
def test(key1,key2):
print(key1,key2)
"""
# 参数是元组的形式还是数据的形式都可以
@pytest.mark.parametrize("name,word", [("zhou1", "问题不大"), ("zhou2", "还好还好")])
def test_parametrize_02(name,word):
print(f'{name}的口头禅是{word}')
@pytest.mark.parametrize("person", [{"name": "zhou1", "word": "问题不大"}, {"name": "zhou2", "word": "还好还好"}])
def test_parametrize_02(person):
print(person["name"])
print(person["word"]) 【来源:国外高防服务器 http://www.558idc.com/stgf.html 欢迎留下您的宝贵建议】