自动化测试

六、Allure测试报告定制

2021-04-05  本文已影响0人  InsaneLoafer

Allure介绍


Allure报表预览

image.png image.png

Allure安装


针对 Pytest 测试框架安装

  1. 测试文件 test_pytest.py 文件如下:
import pytest

def test_success():
    """this test succeeds"""
    assert True


def test_failure():
    """this test fails"""
    assert False


def test_skip():
    """this test is skipped"""
    pytest.skip('for a reason!')


def test_broken():
    raise Exception('oops')
  1. 在pycharm的terminal中切换到当前路径,运行pytest test_allure.py --alluredir=./results/1,就能在目录中生成测试报告文件如下,一个测试用例对应一个 .json 文件:
    image.png
  2. 在terminal中运行allure_practice>allre serve ./results/1 就能自动打开网页查看报告
    image.png

使用 Allure2 生成精美报告

使用上面的例子
  1. 在pycharm的terminal中切换到当前路径,运行pytest test_allure.py --alluredir=./results/1
  2. 在terminal 中运行allure generate ./results/1 -o ./report/1 --clean,会在report目录下生成报告文件如下,包括了.json .txt .html .js .css .ico等文件:
    image.png

Allure常用特性

import pytest
import allure

# 标识整个模块都用来进行登录
@allure.feature('登录模块')
class TestLogin():

    @allure.story('登录成功')
    def test_login_success(self):
        print("这是登录:测试用例,登陆成功")
        pass

    @allure.story('登录失败')
    def test_login_fail(self):
        print("这是登录:测试用例,登陆失败")
        pass

    @allure.story('用户名缺失')
    def test_login_lostname(self):
        print("用户名缺失")
        pass

    @allure.story('密码缺失')
    def test_login_lostsec(self):

        # 对一些关键步骤进行标记
        with allure.step("点击用户名"):
            print("输入用户名")
        with allure.step("点击密码"):
            print("输入密码")
        print("点击登录")
        with allure.step("点击登录之后登录失败"):
            assert "1" == 1
            print("登录失败")
        pass

    @allure.story("登录失败")
    def test_login_failure(self):
        pass
  1. Terminal中输入pytest test_feature_story.py --alluredir=./results/2,生成报告文件
  2. Terminal中输入allure serve ./results/2,打开测试报告
    image.png
    image.png
  3. 指定 feature 运行,执行pytest -v test_feature_story.py --allure-features '登录模块'-v 为打印详细信息
  4. 指定 story 运行,执行pytest -v test_feature_story.py --allure-stories '登录成功'

Allure特性-feature/story

Allure特性-step

Allure特性-issue,testcase

  1. 加入链接示例:@allure.link("www.baidu.com")
import allure

@allure.link("www.baidu.com")
def test_with_link():
    print("这是一条加了链接的测试")
    pass
  1. 给链接取别名:@allure.link("www.baidu.com", name="百度一下")
import allure

@allure.link("www.baidu.com", name="百度一下")
def test_with_link():
    print("这是一条加了链接的测试")
    pass
  1. 加入测试用例的链接:@allure.testcase(TST_CASE_LINK, '登录用例')
import allure

TST_CASE_LINK ='https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.testcase(TST_CASE_LINK, '登录用例')
def test_with_testcase_link():
    print("这是一条测试用例的链接,链接到测试用例里面")
    pass
  1. 通过传入的bugID生成bug链接:@allure.issue('BugID', 链接名'),运行时指定链接地址
import allure

# 测试bug链接,前面为bugID
"""
在运行时加入bug链接: --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
中括号里面就会传bugID
"""
@allure.issue('140', '这是一个issue')
def test_with_issue_link():
    pass

按重要性级别进行一定范围的测试

名称 描述
Blocker级别 中断缺陷(客户端程序无响应,无法执行下一步操作)
Critical级别 临界缺陷(功能点缺失)
Normal级别 普通缺陷(数值计算错误)
Minor级别 次要缺陷(界面错误与UI需求不符)
Trivial级别 轻微缺陷(必输项无提示,或者提示不规范)
  1. 执行严重级别为 normal和critical的测试用例:pytest -v test_severity.py --allure-severities normal,critical --alluredir=./results/7
import allure
def test_with_no_severity_label():
    pass

@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass

@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass

@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):
    def test_inside_the_normal_severity_test_class(self):
        pass

    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
        pass

在pycharm中配置默认的allure参数

配置以后直接鼠标右键-> run 'pytest for ...' 即可


image.png

前端自动化测试-截图

import allure
import pytest

def test_attach_text():
    allure.attach("这是一个纯文本", attachment_type=allure.attachment_type.TEXT)

def test_attach_htlm():
    allure.attach("<body>这是一段html body块</body>", 'html测试块', attachment_type=allure.attachment_type.HTML)

def test_attach_photo():
    allure.attach(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png",
                  name="这是一个图片", attachment_type=allure.attachment_type.PNG)
import allure
import pytest

def test_attach_text():
    allure.attach("这是一个纯文本", attachment_type=allure.attachment_type.TEXT)

def test_attach_htlm():
    allure.attach("<body>这是一段html body块</body>", 'html测试块', attachment_type=allure.attachment_type.HTML)

# 调用图片要使用file方法
def test_attach_photo():
    allure.attach.file(r"D:\Programs\DevOps\Python_Practice\allure_practice\resource\1.png",
                  name="这是一个图片", attachment_type=allure.attachment_type.PNG)

实战:pytest+allure+selenium

import time
import allure
import pytest
from selenium import webdriver

@allure.testcase("http://www/github.com", "测试用例地址")
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data', ['allure', 'pytest', 'unittest'])
def test_steps_demo(test_data):

    with allure.step("打开百度网页"):
        driver = webdriver.Chrome()
        driver.maximize_window() # 浏览器最大化
        driver.get("http://www.baidu.com")

    with allure.step("输入搜索词"):
        driver.find_element_by_id("kw").send_keys(test_data)
        time.sleep(2)
        driver.find_element_by_id("su").click()
        time.sleep(2)

    with allure.step("保存图片"):
        driver.save_screenshot("./resource/b.png")
        allure.attach.file("./resource/b.png", attachment_type=allure.attachment_type.PNG)
        allure.attach('<head></head><body>首页</body>','Attach with HTML type',allure.attachment_type.HTML)

    with allure.step("关闭浏览器"):
        driver.quit()
上一篇 下一篇

猜你喜欢

热点阅读