7.接口自动化测试-Allure报告

1.环境搭建

(1)下载并解压allure.zip,不要用中文路径,将bin目录配置到path环境变量
官网:Allure下载
(2)cmd安装allure-pytest第三方库
pip install allure-pytest
检测是否安装成功
pip show allure-pytest
在这里插入图片描述

(3)安装JDK环境
(4)把pycharm的bin目录添加到环境变量
在这里插入图片描述

2.代码演示

import pytest,os,allure

list=[[1,2],[2,3],[3,5],[6,6]]
# 使用装饰器,只需要设置列表的值即可
class Test1:

    @pytest.mark.parametrize('expected_result,actual_result',list)
    def test_c100(self,expected_result,actual_result):
        assert expected_result==actual_result
    @pytest.mark.parametrize('x,y,z',[[1,2,3],[4,5,6]])
    def test_c101(self,x,y,z):
        assert x+y==z
if __name__ == '__main__':
    #--alluredir:allure路径
    #./report/report生成报告的路径
    #--clean-alluredir如果有旧数据,需要清除
    pytest.main([__file__,'-sv','--alluredir','./report/report','--clean-alluredir'])
    os.system('allure serve ./report/report')

报告结果:
在这里插入图片描述

3.Allure层级

(1)第一层:项目层@allure.epic
(2)第二层:模块层@allure.feature
(3)第三层:接口层@allure.story
(4)第四层:用例层@allure.title

3.1 代码演示

import pytest,os,allure
# -*- coding: utf-8 -*-

@allure.epic('项目名称')
@allure.feature('业务模块名称')
class Test1:
    @allure.story('接口名称1')
    @allure.title('用例标题1')
    def test_c01(self):
        assert 1==2

    @allure.story('接口名称2')
    @allure.title('用例标题2')
    def test_c02(self):
        assert 2==3
if __name__ == '__main__':
    pytest.main([__file__,'-sv','--alluredir','./report/report','--clean-alluredir'])
    os.system('allure serve ./report/report')

在这里插入图片描述

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python接口自动化测试是一种以Python语言为基础的测试技术,在实施中可以使用pytest和allure两个工具来进行接口自动化测试。下面将以登录接口实战为例说明如何使用pytest和allure进行接口自动化测试。 首先,我们需要安装pytest和allure库。可以使用pip命令进行安装,例如: ``` pip install pytest pip install allure-pytest ``` 接下来,我们需要准备接口测试代码。以登录接口为例,我们可以创建一个名为test_login.py的文件,编写登录接口的测试用例。 ```python import requests import pytest import allure @allure.feature("登录接口测试") class TestLogin: @allure.story("正确的用户名密码登录") def test_login_success(self): url = "http://example.com/api/login" data = { "username": "admin", "password": "123456" } response = requests.post(url, data=data) assert response.status_code == 200 assert response.json()["code"] == 0 @allure.story("错误的用户名密码登录") def test_login_failure(self): url = "http://example.com/api/login" data = { "username": "admin", "password": "wrong_password" } response = requests.post(url, data=data) assert response.status_code == 200 assert response.json()["code"] == 1 ``` 在以上代码中,我们使用了pytest和allure库提供的装饰器对测试用例进行标记,以便于生成测试报告。 接下来,我们可以运行测试用例并生成测试报告。使用以下命令: ``` pytest --alluredir=./report ``` 上述命令将会运行test_login.py文件中的测试用例,并将测试结果输出到report目录中。其中,--alluredir参数指定了报告的输出目录。 最后,我们可以通过以下命令生成测试报告: ``` allure serve ./report ``` 运行以上命令后,将会在浏览器中自动打开测试报告,我们可以查看接口测试的结果、错误信息以及其他相关信息。 以上就是使用pytest和allure进行Python接口自动化测试的实战示例,希望对您有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值