前言
allure可以输出非常精美的测试报告,也可以和pytest进行完美结合,不仅可以渲染页面,还可以控制用例的执行。本文我们将介绍Windows系统中如何配置allure环境。
第一步:配置Java环境
因为allure
的运行依赖于Java环境,所以我们需要先配置好Java环境,allure
需要的Java环境为:JDK1.8+,配置好Java环境后,我们可以在cmd中输入where java
命令查看是否成功成功配置好JDK。
第二步:安装allure
-
下载allure安装包,allure下载地址
-
解压Zip包,将allure安装目录下的
allure-2.17.3/bin
配置到系统的环境变量中,如下图:
- 配置环境变量后,我们可以在cmd中输入
allure --version
查看是否成功安装及配置环境变量,如下图所示:
第三步:安装allure-pytest第三方库
我们在完成了allure的环境配置之后,要将allure与pytest结合起来,还需要安装allure-pytest
第三方库,安装命令如下:
pip install allure-pytest
第四步:allure生成测试报告
我们完成了上述步骤之后,就可以使用allure来生成精美的测试报告了,下面是我们使用allure生成报告的简单示例:
测试demo内容如下:
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输入如下命令:
pytest -s test_allure.py --alluredir report
其中report
为报告的目录,运行命令,输出的内容如下:
================================================================================= 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 =============================================================================
生成网页报告
# 启用 allure server 展示测试报告
allure serve report
浏览器将会自动打开网页报告,如下图:
总结
本文主要介绍了Windows系统中如何配置allure环境,并且结合pytest生成美观的测试报告的基本步骤,后续我们将介绍pytest与allure结合的更高级的应用。
标签:allure,Windows,self,用例,pytest,test,def,软件测试 From: https://www.cnblogs.com/hogwarts/p/17834332.html