pytest--allure测试报告

1.安装

  先安装pytest 之后安装allure-pytest

2.用法

  pytest --alluredir=./results 收集执行测试用例,生成测试数据

  allure server ./results 可视化,生成html报告,这种方式直接打开报告,如果我们想直接生成测试报告,

则应该这样:  

  1.生成报告 allure generate ./results/ -o ./report/1 --clean(路劲覆盖)

  2.打开报告 allure open -h 127.0.0.1 -p 8883 ./report/1

 

 3.测试功能 子功能,测试场景,测试步骤,附加信息等

import allure
import pytest
# 测试场景中的测试功能,子功能或者测试场景,测试步骤,包括附加信息
# @allure.feature('功能名称')
# @allure.story('子功能名称')
# @allure.step('步骤细节')
# @allure.attach('具体文本信息')
#
@allure.feature("登录")
class TestLogin:
@allure.story("登录成功")
def test_login_success(self):
print("登录成功")

@allure.story("登录失败")
def test_login_failed(self):
print("登录失败")

@allure.story("密码缺失")
def test_login_withoutpassword(self):
with allure.step("点击用户名"):
print("输入用户名")
with allure.step("点击密码"):
print("不输入密码")
with allure.step("点击登录后,登录失败.."):
assert 1 == '1'
若有多个模块:登录 购物车 搜索等,我们执行运行登录模块我们这样:
  pytest test_feature_story_step.py --allure-features '登录模块'
  pytest test_feature_story_step.py --allure-stories '登录成功'

4.测试链接和等级以及添加附件
import pytest
import allure

# 链接 和 测试等级
class TestDemo:
@allure.link("http://www.baidu.com")
def test_with_link(self):
print("这是一条加了链接的测试")

@allure.link("http://www.baidu.com", name="链接地址隐藏")
def test_with_aliaslink(self):
print("这是一条加了链接的测试")

TEST_CASE_LINK = 'http://www.baidu.com/1101'

@allure.testcase(TEST_CASE_LINK, "登录用例")
def test_with_testcase_link(self):
print("这是测试用例的url")

# 命令:pytest test_6.py --alluredir=./result/test_severity --allure-severities normal,critical
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity(self):
print("test_with_trivial_severity")

@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity(self):
print("test_with_normal_severity")

@allure.severity(allure.severity_level.CRITICAL)
def test_with_critical_severity(self):
print("test_with_critical_severity")

@allure.severity(allure.severity_level.BLOCKER)
def test_with_blocker_severity(self):
print("test_with_blocker_severity")

# 测试添加附件信息
def test_with_attach_text(self):
print("添加附加信息--纯文本")
allure.attach("这是一个纯文本", attachment_type=allure.attachment_type.TEXT)

def test_with_attach_html(self):
print("添加附加信息--html")
allure.attach("<body>这是一个html</body>", "html测试块", attachment_type=allure.attachment_type.HTML)

def test_with_attach_photo(self):
print("添加附加信息--图片")
allure.attach.file("C:\\framework\\pytestTest\\TestPackage\\1.JPG", "这是一个图片",attachment_type=allure.attachment_type.JPG)

  
posted @ 2022-04-17 17:13  千焱  阅读(419)  评论(0编辑  收藏  举报