Skip to content

Allure2 报告中添加用例支持 tags 标签


简介

Allure 报告支持的一些常见 Pytest 特性包括 xfail、skipif、fixture 等。

测试结果会展示特定的标识在用例详情页面。


xfail 标签

  • 应用场景:在测试中需要对错误的场景进行测试,这些测试预期失败,则可以用 xfail 进行标记。
  • 用法:使用装饰器 @pytest.xfail()

示例

import pytest

# 当用例通过时标注为 xfail
@pytest.mark.xfail(condition=lambda: True, reason='这是一个预期失败的用例')
def test_xfail_expected_failure():
    """this test is an xfail that will be marked as expected failure"""
    assert False

# 当用例通过时标注为 xpass
@pytest.mark.xfail
def test_xfail_unexpected_pass():
    """this test is an xfail that will be marked as unexpected success"""
    assert True


skipif 标签

  • 应用场景:在测试中,有一些测试用例已经发现了某些 bug,但还没有修复或开发完成,可以使用 skipif 跳过本项测试。
  • 用法:使用装饰器 @pytest.skipif()

示例

# 跳过用例
@pytest.mark.skipif('2 + 2 != 5', reason='当条件触发时这个用例被跳过 @pytest.mark.skipif')
def test_skip_by_triggered_condition():
    pass


fixture 标签

  • 应用场景:fixture 和 finalizer 是分别在测试开始之前和测试结束之后由 Pytest 调用的实用程序函数。Allure 跟踪每个 fixture 的调用,并详细显示调用了哪些方法以及哪些参数,从而保持了调用的正确顺序。
  • 用法:使用装饰器 @pytest.fixture()

示例

import pytest

@pytest.fixture()
def func1():
    print("这是一个fixture func1前置动作")
    yield
    print("这是一个fixture func1前置动作")

@pytest.fixture()
def func(request):
    #前置动作 -- 相当于setuo
    print("这是一个fixture方法")
    #后置动作 -- 相当于teqrdown
    # 定义一个终结器,teardown动作放在终结器中
    def over():
        print("session级别终结器")

    request.addfinalizer(over)


class TestClass(object):
    def test_with_scoped_finalizers(self,func, func1):
        print("测试用例")


总结

  • Allure 添加 xfail 标签
  • Allure 添加 skipif 标签
  • Allure 添加 fixture 标签