前言
最近在学习python接口自动化,每天抽一点时间把学习的内容总结一下,主要是为了加强记忆(脑子是个好东西,可惜我没有),有错漏的地方大家给指出来,欢迎一起学习交流~
allure是一个开源的测试报告框架,pytest+allure实现运行指定测试用例,运行完生成html测试报告。
安装allure
mac打开终端
brew install allure
安装完成,执行allure --version,显示版本号即安装成功
自定义测试报告
在原来测试用例test_wework.py上,增加@allure.XX()即可
from common.wework import Wework
import allure
#生成报告,查看报告
#运行测试用例:pytest test_wework.py --clean-alluredir --alluredir ./../report
#直接查看报告:allure serve ./../report
#生成报告:allure generate ./../report -o ./../html --clean
@allure.testcase("https://www.baidu.com/")
@allure.epic("通讯录")
@allure.feature("企业微信测试")
class TestWework:
@allure.story("获取员ID列表")
@allure.title("获取成员ID列表-输入为空")
def test_post_list_id(self):
print(Wework().test_post_list_id(""))
@allure.story("手机号获取userid")
@allure.title("手机号获取userid-输入正确手机号")
@allure.severity(allure.severity_level.BLOCKER)
def test_post_getuserid1(self):
print(Wework().test_post_getuserid("13126966988"))
@allure.story("手机号获取userid")
@allure.description("这条用例是为了验证...巴拉巴拉...的场景")
@allure.title("手机号获取userid-手机号为空")
@allure.severity(allure.severity_level.NORMAL)
def test_post_getuserid2(self):
print(Wework().test_post_getuserid(""))
allure的装饰器有很多,详情见博客
https://blog.csdn.net/wenxiaoba/article/details/127295970
生成html
怎么生成测试报告呢?通过main.py实现
import os
import pytest
if __name__ == '__main__':
pytest.main(["-s","./case/test_wework.py","--clean-alluredir","--alluredir","./temp"])
os.system("allure generate ./temp -o ./report --clean")
pytest.main([“-s”,“-v”,“-x”])
等同于控制台输入:pytest -s -v -x
pytest.main([“-s”,“./case/test_wework.py”,“–clean-alluredir”,“–alluredir”,“./temp”])
等同于控制台输入:pytest -s ./case/test_wework.py --clean-alluredir --alluredir ./temp
os.system是将字符串转化为命令在服务器上运行。