软件测试|Windows系统配置pytest+allure环境教程

前言

allure可以输出非常精美的测试报告,也可以和pytest进行完美结合,不仅可以渲染页面,还可以控制用例的执行。本文我们将介绍Windows系统中如何配置allure环境。

第一步:配置Java环境

因为allure的运行依赖于Java环境,所以我们需要先配置好Java环境,allure需要的Java环境为:JDK1.8+,配置好Java环境后,我们可以在cmd中输入where java命令查看是否成功成功配置好JDK。

第二步:安装allure

  1. 下载allure安装包,allure下载地址

  2. 解压Zip包,将allure安装目录下的 allure-2.17.3/bin配置到系统的环境变量中,如下图:

  1. 配置环境变量后,我们可以在cmd中输入allure --version查看是否成功安装及配置环境变量,如下图所示:

第三步:安装allure-pytest第三方库

我们在完成了allure的环境配置之后,要将allure与pytest结合起来,还需要安装allure-pytest第三方库,安装命令如下:

python 复制代码
pip install allure-pytest

第四步:allure生成测试报告

我们完成了上述步骤之后,就可以使用allure来生成精美的测试报告了,下面是我们使用allure生成报告的简单示例:

测试demo内容如下:

python 复制代码
import allure


@allure.step("步骤1:step1")
def step_1():
    print("step1")


@allure.step("步骤2:step2")
def step_2():
    print("step2")


@allure.feature("test")
class TestEditPage():
    @allure.story("test01")
    def test_1(self):
        """这是成功用例"""
        step_1()
        step_2()
        print("测试成功")

    @allure.story("test02")
    def test_2(self):
        """这是失败用例"""
        assert 1 == 2, "测试失败"

    @allure.story("test03")
    def test_3(self):
        """成功用例3"""
        print("test03")

    def test_31(self):
        """成功用例31"""
        print("成功用例31")

    def test_32(self):
        """成功用例32"""
        print("成功用例32")

    @allure.story("test04")
    def test_04(self):
        """成功用例4"""
        print("test04")

    def test_41(self):
        """失败用例41"""
        print("失败用例41")
        assert 1 == 2

    def test_42(self):
        """成功用例42"""
        print("成功用例42")

我们先切换到demo所在目录,在Terminal输入如下命令:

python 复制代码
pytest -s test_allure.py --alluredir report

其中report为报告的目录,运行命令,输出的内容如下:

python 复制代码
================================================================================= test session starts =================================================================================
platform win32 -- Python 3.7.7, pytest-7.4.0, pluggy-1.2.0
rootdir: C:\Users\89703\PycharmProjects\hogwarts-sdetck26\CalculatorProject
plugins: allure-pytest-2.13.2, Faker-18.13.0
collected 8 items                                                                                                                                                                      

test_allure.py step1
step2
测试成功
.Ftest03
.成功用例31
.成功用例32
.test04
.失败用例41
F成功用例42
.

====================================================================================== FAILURES =======================================================================================
_________________________________________________________________________________ TestEditPage.test_2 _________________________________________________________________________________

self = <CalculatorProject.test_allure.TestEditPage object at 0x000001E1A96F7308>

    @allure.story("test02")
    def test_2(self):
        """这是失败用例"""
>       assert 1 == 2, "测试失败"
E       AssertionError: 测试失败
E       assert 1 == 2

test_allure.py:33: AssertionError
________________________________________________________________________________ TestEditPage.test_41 _________________________________________________________________________________

self = <CalculatorProject.test_allure.TestEditPage object at 0x000001E1A96F5AC8>

    def test_41(self):
        """失败用例41"""
        print("失败用例41")
>       assert 1 == 2
E       assert 1 == 2

test_allure.py:56: AssertionError
=============================================================================== short test summary info ===============================================================================
FAILED test_allure.py::TestEditPage::test_2 - AssertionError: 测试失败
FAILED test_allure.py::TestEditPage::test_41 - assert 1 == 2
============================================================================= 2 failed, 6 passed in 0.31s =============================================================================

生成网页报告

python 复制代码
# 启用 allure server 展示测试报告

allure serve report

浏览器将会自动打开网页报告,如下图:

总结

本文主要介绍了Windows系统中如何配置allure环境,并且结合pytest生成美观的测试报告的基本步骤,后续我们将介绍pytest与allure结合的更高级的应用。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
阿白的白日梦10 小时前
winget基础管理---更新/修改源为国内源
windows
埃博拉酱4 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest5 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技5 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~5 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
qq_459234425 天前
【题库】| 商用密码应用安全性评估从业人员考核题库(四十)
职场和发展·密码学·学习方法·考核·商用密码·商用密码应用安全性评估·密评
敲敲了个代码5 天前
[特殊字符] 空数组的迷惑行为:为什么 every 为真,some 为假?
前端·javascript·react.js·面试·职场和发展