在生成报告前,需要先记录下最近学习到的pytest的配置,在项目的根目录添加pytest.ini文件,然后填入
[pytest]
addopts = -vs #命令行的参数,用空格隔开
testpaths = ./ #测试用例的路径
python_files = test_*.py #文件名的规则
python_classes = Test* #类名的规则
python_functions = test #方法名的规则
makers = #设置用例的分组
smoke:'用例'
usermanage
这里你可以根据自己的需求去修改配置
安装Allure好了,开始安装我们的allure了
下载地址:Central Repository: io/qameta/allure/allure-commandline
下载需要的版本
下载解压完成后,将bin文件夹所在的目录放入环境变量中,同时需要安装JDK
完成后,命令行pip install allure-pytest安装allure插件,安装完成后可以验证一下,看看是否安装成功,首次安装后需要重启下pycharm
生成测试报告上代码
import pytest
import requests
import yaml
import json
import os
import allure
from load_data import yaml_load
token = None
@pytest.mark.parametrize('data1',yaml_load.load('../data/user.yaml'))
def test_01(data1):
url_login = "https:/jlcloud/api/login"
response1 = requests.request("POST", url=url_login, json=data1['user'])
print(response1.text)
result=json.loads(response1.text)
try:
global token #定义全局变量
token=result['data']
print(token)
except:
pass
assert data1['msg'] == result['message']
# 获取用户信息
def test_02():
print(token)
url_userinfo = "https:/jlcloud/api/login/getUserInfo"
params = {"token": token}
res2 = requests.request("GET", url=url_userinfo, params=params)
print(res2.text)
result2 = json.loads(res2.text)
assert result2['message'] == "成功"
if __name__ == '__main__': # pytest运行模式
pytest.main('-vs',--alluredir= ./temp") #生成json数据,生成后可以在temp文件查看
os.system('allure generate ./pytest_case/temp -o report/html --clean') #将json数据生成,报告存至report文件
生成的json数据如下:
在report中点击index.html文件用浏览器打开,测试报告如下:
这里我直接运行了文件,不知道为啥没有生成报告,但是使用终端命令行 allure generate ./pytest_case/temp -o report/html --clean却可以生成
另一种方式将pytest.ini配置文件的addopts稍作修改,就不需要每次运行都要写上参数了
[pytest] addopts = -vs --alluredir ./temp #生成json数据 testpaths= ./pytest_case python_files=test_*.py python_classes=Test* python_functions= test
import pytest
import requests
import yaml
import json
import os
import allure
from load_data import yaml_load
token = None
@pytest.mark.parametrize('data1',yaml_load.load('../data/user.yaml'))
def test_01(data1):
url_login = "https:/jlcloud/api/login"
response1 = requests.request("POST", url=url_login, json=data1['user'])
print(response1.text)
result=json.loads(response1.text)
try:
global token #定义全局变量
token=result['data']
print(token)
except:
pass
assert data1['msg'] == result['message']
# 获取用户信息
def test_02():
print(token)
url_userinfo = "https:/jlcloud/api/login/getUserInfo"
params = {"token": token}
res2 = requests.request("GET", url=url_userinfo, params=params)
print(res2.text)
result2 = json.loads(res2.text)
assert result2['message'] == "成功"
if __name__ == '__main__': # pytest运行模式
pytest.main()
os.system('allure generate ./pytest_case/temp -o report/html --clean')#将json数据生成报告



