参数化(parametrize)可以实现测试数据和测试脚本完全分离,便于测试的维护和扩展
如何在测试报告中展示参数化的用例:
- pytest可以在参数化的时候加 ids 参数对每个用例说明使用场景
- allure.title() 可以接收测试数据中的参数对每个用例说明使用场景,用例说明可以同测试维护在一起,数据可以一次性读取,更加方便
- allure.dynamic.title() 可以在用例里面使用,作用同allure.title()
测试脚本
- 新增测试文件:test_parametrize.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : test_parametrize.py
# @Author: xxxx xxxx
# @Date : 2021/10/9 23:38
# @Desc :
import pytest
import allure
def login(username, password):
"""登录功能"""
print("输入账号:%s" % username)
print("输入密码:%s" % password)
# 返回
return {"code": 0, "msg": "success!"}
# 测试数据
# 从excel 或者 mysql 中获取
test_data = [
({"username": "user1", "password": "123456"}, "success!", "输入正确账号,密码,登录成功"),
({"username": "user2", "password": "123456"}, "failed!", "输入错误账号,密码,登录失败"),
({"username": "user3", "password": "123456"}, "success!", "输入正确账号,密码,登录成功"),
]
@allure.story("登录用例")
@allure.title("{title}")
@pytest.mark.parametrize("test_input,expected,title",
test_data
)
def test_login(test_input, expected, title):
"""
# 登录测试用例
# 测试用例的参数 比待测试功能的参数 多一个{title}
# 参数化时传 3个参数@pytest.mark.parametrize "test_input,expected,title"
# @allure.title("{title}") 的参数写法 "{title}"
"""
# 获取函数返回结果
result = login(test_input["username"], test_input["password"])
# 断言
assert result["msg"] == expected
# @allure.story("登录用例")
# @pytest.mark.parametrize("test_input,expected,title",
# test_data
# )
# def test_login(test_input, expected, title):
# """
# # 登录测试用例
# # 测试用例的参数 比待测试功能的参数 多一个{title}
# # 参数化时传 3个参数@pytest.mark.parametrize "test_input,expected,title"
# # allure.dynamic.title(title) # 更符合平时使用习惯
# 其他动态生成的属性
# #allure.dynamic.feature
#allure.dynamic.link
#allure.dynamic.issue
#allure.dynamic.testcase
#allure.dynamic.story
#allure.dynamic.title
#allure.dynamic.description
# """
# # 获取函数返回结果
# result = login(test_input["username"], test_input["password"])
# # 断言
# assert result["msg"] == expected
# allure.dynamic.title(title)
运行用例
- 在terminal通过如下命令运行用例:
$ pytest test_parametrize.py --alluredir ./report/allure_report --clean-alluredir
$ allure serve ./report/allure_report
测试报告
参考文档:https://www.cnblogs.com/yoyoketang/p/12853605.html