栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python做接口测试的学习记录day4——使用Allure生成测试报告】

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python做接口测试的学习记录day4——使用Allure生成测试报告】

在生成报告前,需要先记录下最近学习到的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数据生成报告

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号