参数化可以组装测试数据,在测试前定义好测试数据,并在测试用例中使用
# 单参数循环
@pytest.mark.parametrize("a",["b","c"])
def test_parametrize(a):
print(a)
# 多参数循环
@pytest.mark.parametrize("a,b",[("c","d"),("e","f")])
def test_parametrize(a,b):
print(a,b)
单参数循环
import pytest
# 单参数单次循环
@pytest.mark.parametrize("key",["value"])
def test_parametrize01(key):
print(key)
if __name__=="__main__":
pytest.main()
import pytest
# 单参数,多次循环
@pytest.mark.parametrize("key",["value1","value2","value3"])
def test_parametrize01(key):
print(key)
if __name__=="__main__":
pytest.main()
from time import sleep
import pytest
# 单参数,单次循环
# @pytest.mark.parametrize("key",["value"])
# def test_parametrize01(key):
# print(key)
# 单参数,多次循环。数组里面有几组数据,循环几次
# @pytest.mark.parametrize("test", ['接口自动化', 'UI自动化', '性能测试'])
# def test_parametrize02(test):
# assert test == "接口自动化"
from selenium.webdriver.common.by import By
from utils.read import read_yaml
@pytest.mark.parametrize("key", read_yaml()['skill'])
def test_baidu(driver, key):
driver.get("https://www.baidu.com/")
driver.find_element(By.ID, 'kw').send_keys(key)
driver.find_element(By.ID, 'su').click()
sleep(1)
多参数循环, 常用于登录、注册场景
from time import sleep
import allure
import pytest
from selenium.webdriver.common.by import By
from utils.read import read_yaml
heros = [("安琪拉", "我的小熊去哪里了"), ("后裔", "周日被我射熄火了")]
# 多参数多次循环
@pytest.mark.parametrize("hero,word", heros)
def test_parametrize02(hero, word):
print("{}的台词是{}".format(hero, word))
@pytest.mark.parametrize("key,value", [["安琪拉", "拉"], ["后裔", "后"]])
def test_parametrize(key, value):
assert value in key
# @pytest.mark.parametrize("data", read_yaml()['userinfos'])
# def test_login(driver, data):
# driver.get("http://sellshop.5istudy.online/sell/user/login_page")
# driver.find_element(By.ID, 'username').send_keys(data['username'])
# driver.find_element(By.ID, 'password').send_keys(data['password'])
# driver.find_element(By.CSS_SELECTOR, '#login > form > p.login.button > input[type=submit]').click()
# sleep(2)
@pytest.mark.parametrize("username,password", read_yaml()['userinfo_list'])
def test_login(driver, username,password):
driver.get("http://sellshop.5istudy.online/sell/user/login_page")
driver.find_element(By.ID, 'username').send_keys(username)
driver.find_element(By.ID, 'password').send_keys(password)
driver.find_element(By.CSS_SELECTOR, '#login > form > p.login.button > input[type=submit]').click()
if username == 'admin':
text = driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div > strong").text
assert text == "用户不存在"
sleep(2)
参数值为字典形式
# 参数值为字典形式
@pytest.mark.parametrize("hero",[{"name":"安琪拉"}])
def test_parametrize(hero):
print(hero["name"])
import pytest
@pytest.mark.parametrize("data", [{"name": "安琪拉", "word": "火焰是我最喜欢的玩具"}])
def test_parametrize03(data):
print(data["name"])
print(data["word"])
网友评论