allure 报告

一、简介

二、下载安装

三、报告生成

四、环境配置

五、Python 使用 allure 方法


一、简介

官方文档:https://docs.qameta.io/allure/

二、下载安装

1、linux 下载安装

  1. 先检查是否安装npm:     which npm
  2. 未安装npm的话:curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -
  3. 安装:yum install -y nodejs
  4. # npm install -g cnpm --registry=https://registry.npm.taobao.org
  5. 查看当前npm的镜像源:npm config get registry
  6. 修改npm镜像源:npm config set registry https://registry.npm.taobao.org
  7. 安装: npm install
  8. npm run build
  9. 查看版本:npm -v
  10. 下载安装allure : https://github.com/allure-framework/allure2/releases/tag/2.7.0
  11. 上一步无法下载的话 使用:wget https://registry.npmjs.org/allure-commandline/-/allure-commandline-2.13.0.tgz
  12. 解压后 配置环境变量
  13. 查看安装版本:allure --version

2、windows 下载安装

下载地址

下载后解压到安装目录,配置环境变量

查看安装版本:allure --version

三、报告生成

1、结合pytest框架生成 xml格式的报告数据   

python -m pytest --alluredir=[allure的xml目录]

2、将xml报告数据转换成html格式

allure generate --clean [allure的xml目录] -o [allure的html目录]

3、打开报告-默认浏览器

allure open <directory-with-report>

4、清理报告

使用 allure report clean 命令。

四、环境配置

---------方式一-------------

1、在报告生成前创建 environment.properties 环境配置文件

2、environment.properties 文件内配置环境属性

Browser=Chrome
Browser.Version=63.0
Stand=Production

3、将 环境配置文件 environment.properties 移动到报告目录下以后 生成报告即可

-------方式二--------

1、在报告生成前创建 environment.xml 环境配置文件

2、environment.xml 文件内配置环境属性

<environment>
    <parameter>
        <key>Browser</key>
        <value>Chrome</value>
    </parameter>
    <parameter>
        <key>Browser.Version</key>
        <value>63.0</value>
    </parameter>
    <parameter>
        <key>Stand</key>
        <value>Production</value>
    </parameter>
</environment>

3、将环境配置文件environment.xml 移动到报告目录下以后 生成报告即可

五、Python 使用 allure 方法

1、安装 allure-pytest 库

pip install allure-pytest

2、在测试完成后查看报告  此命令将在默认浏览器中显示您生成的报告。

allure serve /tmp/my_allure_results

3、用例描述 在用例方法内使用"""用例描述""" 

import pytest

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

4、标记预期失败的方法   @pytest.mark.xfail(condition=lambda: True, reason='this test is expecting failure')

@pytest.mark.xfail(condition=lambda: True, reason='this test is expecting failure')
def test_xfail_expected_failure():
    """this test is an xfail that will be marked as expected failure"""
    assert False

5、条件标记

@pytest.mark.skipif('2 + 2 != 5', reason='This test is skipped by a triggered condition in @pytest.mark.skipif')
def test_skip_by_triggered_condition():
    pass

6、参数化

import allure
import pytest


@allure.step
def simple_step(step_param1, step_param2 = None):
    pass


@pytest.mark.parametrize('param1', [True, False], ids=['id explaining value 1', 'id explaining value 2'])
def test_parameterize_with_id(param1):
    simple_step(param1)


@pytest.mark.parametrize('param1', [True, False])
@pytest.mark.parametrize('param2', ['value 1', 'value 2'])
def test_parametrize_with_two_parameters(param1, param2):
    simple_step(param1, param2)


@pytest.mark.parametrize('param1', [True], ids=['boolean parameter id'])
@pytest.mark.parametrize('param2', ['value 1', 'value 2'])
@pytest.mark.parametrize('param3', [1])
def test_parameterize_with_uneven_value_sets(param1, param2, param3):
    simple_step(param1, param3)
    simple_step(param2)

7、步骤描述

import allure

@allure.step('Step with placeholders in the title, positional: "{0}", keyword: "{key}"')
def step_with_title_placeholders(arg1, key=None):
    pass


def test_steps_with_placeholders():
    step_with_title_placeholders(1, key='something')
    step_with_title_placeholders(2)
    step_with_title_placeholders(3, 'anything')

8、夹具步骤描述  使用conftest.py模块中定义的夹具进行测试的示例(即使未直接导入,Pytest也会解析此类夹具)

# conftest.py
import allure
import pytest


@allure.step('step in conftest.py')
def conftest_step():
    pass


@pytest.fixture
def fixture_with_conftest_step():
    conftest_step()
# test case file
import allure

from .steps import imported_step


@allure.step
def passing_step():
    pass


def test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):
    passing_step()

9、添加附件

allure.attach(body, name, attachment_type, extension)

  1. body -要写入文件的原始内容。

  2. name -带有文件名的字符串

  3. attachment_type-allure.attachment_type值之一

  4. extension -提供的-将用作创建文件的扩展名。

10、添加附件方法二

allure.attach.file(source, name, attachment_type, extension)

  1. source -包含文件路径的字符串。

  2. name -带有文件名的字符串

  3. attachment_type-allure.attachment_type值之一

  4. extension -提供的-将用作创建文件的扩展名。

import allure
import pytest


@pytest.fixture
def attach_file_in_module_scope_fixture_with_finalizer(request):
    allure.attach('A text attacment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT)
    def finalizer_module_scope_fixture():
        allure.attach('A text attacment in module scope finalizer', 'blah blah blah blah',
                      allure.attachment_type.TEXT)
    request.addfinalizer(finalizer_module_scope_fixture)


def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_finalizer):
    pass


def test_multiple_attachments():
    allure.attach.file('./data/totally_open_source_kitten.png', attachment_type=allure.attachment_type.PNG)
    allure.attach('<head></head><body> a page </body>', 'Attach with HTML type', allure.attachment_type.HTML)

11、用例描述

@allure.description提供描述字符串的装饰器,也可以用于@allure.description_html提供一些HTML,以在测试用例的“描述”部分中呈现

12、标题

import allure
import pytest


@allure.title("This test has a custom title")
def test_with_a_title():
    assert 2 + 2 == 4

13、链接

import allure

TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'


@allure.link('https://www.youtube.com/watch?v=4YYzUTYZRMU')
def test_with_link():
    pass


@allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name='Click me')
def test_with_named_link():
    pass


@allure.issue('140', 'Pytest-flaky test retries shows like test steps')
def test_with_issue_link():
    pass


@allure.testcase(TEST_CASE_LINK, 'Test case title')
def test_with_testcase_link():
    pass

14、失败重试

使用插件 pip install pytest-rerunfailures

pytest --reruns 5 --reruns-delay 1  # 使用pytest 命令行 指定失败重试次数 可选重试延迟时间 秒

15、BDD标记 

 @allure.feature (修饰类)@allure.story(修饰方法) 用于根据特定于项目的功能/故事细分来标记测试   

要标记某些功能或故事属于史诗,请使用以epic_前缀开头的名称

import allure


def test_without_any_annotations_that_wont_be_executed():
    pass


@allure.story('epic_1')
def test_with_epic_1():
    pass


@allure.story('story_1')
def test_with_story_1():
    pass

@allure.story('story_2')
def test_with_story_2():
    pass


@allure.feature('feature_2')
@allure.story('story_2')
def test_with_story_2_and_feature_2():
    pass

可以使用以下命令行选项来指定不同的测试集,以执行传递逗号分隔值列表的操作:

  1. --allure-epics

  2. --allure-features

  3. --allure-stories

17、严重性标记

使用@allure.severity装饰器。它以allure.severity_level枚举值作为参数

通过将--allure-severities命令行选项与逗号分隔的严重性级别结合使用,将仅运行具有相应严重性的测试

pytest tests.py --allure-severities normal,critical
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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值