视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:pytest接口自动化测试框架 | 汇总_COCOgsta的博客-CSDN博客
import pytest
from selenium import webdriver
from time import sleep
"""
pytest参数化
当一组测试用例有固定的测试数据时,就可以通过参数化的方式简化测试用例的编写。
通过pytest.mark.parametrize()方法设置参数:
参数名:"user,pw,expected"用来定义参数的名称
参数值:通过数组定义参数值时,每一个元组都是一条测试用例的测试数据
ids参数:默认None,用来重新定义测试用例的名称
"""
@pytest.mark.parametrize(
"user,pw,expected",
[("xuzhu666", "123456", "xuzhu666,欢迎来到"),
("zz666", "123456", "zz666,欢迎来到")],
ids=["case1","case2"])
def test_login(user,pw,expected):
driver = webdriver.Chrome()
driver.get("http://39.98.138.157/shopxo/")
driver.find_element_by_link_text("登录").click()
driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/div[2]/form/div[1]/input").send_keys(user)
driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div[2]/form/div[2]/input').send_keys(pw)
driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/div[2]/form/div[3]/button").click()
sleep(3)
# == 测试相等
# 登录成功检查
welcome = driver.find_element_by_xpath("//*[contains(text(),'欢迎来到')]").text
assert expected == welcome
driver.quit()
if __name__ == '__main__':
pytest.main(['-s', "test_param.py"])
运行结果
C:\Users\guoliang\AppData\Local\Programs\Python\Python36\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --target test_param.py::test_login
Testing started at 18:10 ...
Launching pytest with arguments test_param.py::test_login --no-header --no-summary -q in D:\SynologyDrive\CodeLearning\pytest\base_used
============================= test session starts =============================
collecting ... collected 2 items
test_param.py::test_login[case1]
test_param.py::test_login[case2]
============================= 2 passed in 36.90s ==============================
Process finished with exit code 0
PASSED [ 50%]PASSED [100%]