How to use allure to generate test reports in python automation

The Allure test report framework helps you easily achieve "high-level" report presentation. This article demonstrates how to integrate the Allure test framework from 0 to 1 with examples. It focuses on how to integrate Allure into an existing automated test project and how to optimize the display of reports. Allure is very powerful and supports multiple
related topics: javapythonwww.cppcns.comnbsp; (If the Homebrew version is old, otherwise the installed allure version is also very old. You need to upgrade the Homebrew
Allure test report framework to help you easily realize the "high-level" report display. This article passes The example demonstrates how to integrate the Allure test framework from 0 to 1. It focuses on how to integrate Allure into an existing automated test project and how to optimize the display of reports. Allure is very powerful and supports multiple languages ​​and multiple test frameworks, no matter Whether it is java/python or Junit/TestNG, the implementation process of other languages ​​or frameworks is the same as this article, and the specific configuration refers to the framework specifications of each language

Install

install allure

Windows users:

scoop install allure (you need to download and install Scoop first, this method does not need to configure environment variables)
MAC users:

Automatic installation through Homebrew
brew install allure &www.cppcns.comnbsp; (If the Homebrew version is old, you need to upgrade Homebrew first, otherwise the installed allure version is also very old and may not be compatible with the Python plugin)
Manual installation:

You can download it manually from the official website https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
The latest version is 2.13.6
How to use allure to generate test reports in python automation

After downloading, unzip and configure environment variables

Install the allure-pytest plugin

pip install allure-pytest

allure common features

Want to see test features, sub-features or scenarios in the report, test steps, including test additional information can use @feature, @story, @step, @attach

step:

import allure
function plus @allure.feature("feature name")
sub-feature plus @allure.story("sub-feature name")
step plus @allure.step("step details")
@allure.attach("specific Text information"), additional information is required, which can be data, text, pictures, videos, web pages.
If you only test some functions, you can add restrictions and filters:
pytest file name --allure-features "The name of the function that needs to be run"

allure feature—feature/story

@allure.feature vs @allure.store

A feature is equivalent to a function, a large module. It classifies cases into a feature, and the report is displayed in the behavior. It is equivalent to a testsuite
story, which corresponds to different scenarios under this function or module. Branch functions belong to the feature. The structure of the report is displayed in the features, which is equivalent to the testcase
feature and story similar to the parent and child relationship
step feature

Each step in the testing process is generally placed in a specific logical method, but
can be placed in key steps, displayed in the report in
the app, web automated test, it is recommended to switch to a new page as a step
usage:
@allure.step () can only be placed on a class or method in the form of a decorator
with allure.step(): it can be placed in a test case method, but the code of the test step needs to be included in the statement to
run:

Collect results during test execution of www.cppcns.com

pytest [test file] -s -q --alluredir=./result --clean-alluredir

--alluredir This option is used to specify the path to store the test results
--clean-alluredir This option is used to clear the previously generated results
View the test report:

Method 1: View the actual report after the test is completed, watch the report online, and directly open the default browser to display the current report

allure serve ./result

Method 2: Generate a report from the result, which is a service that starts tomcat, which requires two steps

Generate report:

allure generate ./result -o ./report --clean (Note: --clean is used to clear previously generated reports)

Open the report:

allure open -h 127.0.0.1 -p 8883 ./report (this method directly generates a tomcat service that can be accessed remotely)

for example:

There are the following code files

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_allure.py
@time:2020/10/10
"""
import allure
import pytest


@allure.feature('登录模块')
class TestLogin():
    @allure.story('登录成功')
    @allure.title('登录成功标题')
    def test_login_sucess(self):
        with allure.step('步骤1:打开应用'):
            print('应用已打开')
        with allure.step('步骤2:进入登录页面'):
            print('登录页面已打开')
        with allure.step('步骤3:输入用户名和密码'):
            print('用户名和密码输入成功')
        print('登录测试用例:登录成功')

    @allure.story('登录成功')
    def test_login_sucess2(self):
        assert '1' == 1
        print('登录测试用例:登录成功')

    @allure.story('登录失败')
    def test_login_failure_a(self):
        print('登录测试用例:登录失败,用户名缺失')

    @allure.story('登录失败')
    def test_login_failure_b(self):
        print('登录测试用例:登录失败,密码缺失')

    @allure.story('登录失败')
    def test_login_failure_c(self):
        with allure.step('输入用户名'):
            print('已输入用户名')
        with allure.step('输入密码'):
            print('已输入密码')
        with allure.step('点击登录'):
            print('已点击登录')
        print('登录测试用例:登录失败,密码错误')


@allure.feature('搜索模块')
class TestSearch():
    def test_search1(self):
        print('搜索用例1')

    TEST_CASE_LINK = 'https://mirrors.huaweicloud.com/'
    @allure.testcase(TEST_CASE_LINK,'测试用例连接')
    def test_search2(self):
        print('搜索用例2')
    @allure.step('搜索步骤')
    def test_search3(self):
        print('搜索用例3')

Execute the commands in sequence:

pytest test_allure.py --alluredir=./result --clean-alluredir

allure serve ./result

chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_allure.py --alluredir=./result --clean-alluredir
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18
collected 8 items                                                                                                                                                             

test_allure.py .F......                                                                                                                                                 [100%]

================================================================================== FAILURES ===================================================================================
________________________________________________________________________ TestLogin.test_login_sucess2 _________________________________________________________________________

self = <test_allure.TestLogin object at 0x7fef3d5cba90>

    @allure.story('登录成功')
    def test_login_sucess2(self):
>       assert '1' == 1
E       AssertionError: assert '1' == 1

test_allure.py:27: AssertionError
=========================================================================== short test summary info ===========================================================================
FAILED test_allure.py::TestLogin::test_login_sucess2 - AssertionError: assert '1' == 1
========================================================================= 1 failed, 7 passed in 0.07s =========================================================================
chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result 
Generating report to temp directory...
Report successfully generated to /var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/7024790777193223986/allure-report
Starting web server...
2020-10-13 21:39:56.174:INFO::main: Logging initialized @6818ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.12.100:58977/>. Press <Ctrl+C> to exit

Generated report:

How to use allure to generate test reports in python automation

allure-feature-testcase

Associated test cases (you can directly link to the address of the test case)

example:

TEST_CASE_LINK = 'https://mirrors.huaweicloud.com/'
@allure.testcase(TEST_CASE_LINK,'测试用例连接')
def test_search(self):
    print('搜索用例')

How to use allure to generate test reports in python automation

Scope testing by level of importance

Usually the tests include P0, smoke test, and verification on-line test. Executed according to the level of importance, for example, the main process and important modules should be run once when going online, which can be solved by the following methods

By appending the @pytest.mark tag

via allure.feature, allure.story

You can also attach tags through allure.severity

Level:
trivial: not important, minor defect (must be entered without prompt, or prompt is irregular)
minor not very important, minor defect (interface error does not match UI requirements)
normal: normal problem, common defect (numerical calculation error)
critical : Serious, critical defect (missing function point)
blocker: blocking, interruption defect (the client program does not respond and cannot perform the next operation)
Usage:

Add @allure.severity(allure.severity_level.TRIVIAL) above methods, functions and classes

implement:

pytest -s -v filename --allure-severities normal,critical

for example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_severity.py
@time:2020/10/11
"""
import allure
import pytest


# 不加任何标记,默认normal
def test_with_no_severity():
    pass


# trivial:不重要,轻微缺陷(必输项无提示,或者提示不规范)
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass


# minor 级别 不太重要,次要缺陷(界面错误与UI需求不符)
@allure.severity(allure.severity_level.MINOR)
def test_with_minor_severity():
    pass


# normal:正常问题,普通缺陷(数值计算错误)
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass


# critical:严重,临界缺陷(功能点缺失)
@allure.severity(allure.severity_level.CRITICAL)
def test_with_ritical_severity():
    pass


# blocker:阻塞,中断缺陷(客户端程序无响应,无法执行下一步操作)
@allure.severity(allure.severity_level.BLOCKER)
def test_with_blocker_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):

    # 不加任何标记,默认为同class级别
    def test_inside_with_normal_severity(self):
        pass

    # 重新设置了critical级别
    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_with_critical_severity(self):
        pass

implement:

chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_severity.py --alluredir=./result --clean-alluredir -vs
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18
collected 8 items                                                                                                                                                             

test_severity.py::test_with_no_severity PASSED
test_severity.py::test_with_trivial_severity PASSED
test_severity.py::test_with_minor_severity PASSED
test_severity.py::test_with_normhttp://www.cppcns.comal_severity PASSED
test_severity.py::test_with_ritical_severity PASSED
test_severity.py::test_with_blocker_severity PASSED
test_severity.py::TestClassWithNormalSeverity::test_inside_with_normal_severity PASSED
test_severity.py::TestClassWithNormalSeverity::test_inside_with_critical_severity PASSED

============================================================================== 8 passed in 0.03s ========================================================================www.cppcns.com======
chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result 
Generating report to temp directory...
Report successfully generated to /var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/17788207943997663035/allure-report
Starting web server...
2020-10-13 22:27:49.842:INFO::main: Logging initialized @6620ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.12.100:59696/>. Press <Ctrl+C> to exit
python自动化之如何利用allure生成测试报告

Ultimate Use Case:
Baidu Search:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_baidudemo.py
@time:2020/10/13
"""
import pytest
import allure
from selenium import webdriver
import time

@allure.testcase('https://www.github.com')
@allure.feature("百度搜索")
@pytest.mark.parametrize('test_data1',['allure','pytest','unittest'])
def test_steps_demo(test_data1):
    with allure.step('打开百度网页'):
        driver=webdriver.Chrome()
        driver.get('http://www.baidu.com')
        driver.maximize_window()
    with allure.step(f'输入搜索词:{test_data1}'):
        driver.find_element_by_id('kw').send_keys(test_data1)
        time.sl编程客栈eep(2)
        driver.find_element_by_id('su').click()
        time.sleep(2)
    with allure.step('保存图片'):
        driver.save_screenshot('./screenshot/baidu.png')
        allure.attach.file('./screenshot/baidu.png',attachment_type=allure.attachment_type.PNG)
    with allure.step('关闭浏览器'):
        driver.quit()

implement:

chenshifengdeMacBook-Pro:testcode chenshifeng$ pytest test_baidudemo.py --alluredir=./result --clean-alluredir -vs
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.9.0, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/chenshifeng/MyCode/PythonCode/SFDSZL/test_pytest, configfile: pytest.ini
plugins: allure-pytest-2.8.18
collected 3 items                                                                                                                                                             

test_baidudemo.py::test_steps_demo[allure] PASSED
test_baidudemo.py::test_steps_demo[pytest] PASSED
test_baidudemo.py::test_steps_demo[unittest] PASSED

============================================================================= 3 passed in 24.65s ==============================================================================
chenshifengdeMacBook-Pro:testcode chenshifeng$ allure serve ./result 
Generating report to temp directory...
Report successfully generated to /var/folders/p0/3_7fwrvx6n3ftpfd4wjb01300000gn/T/18005664130273264423/allure-report
Starting web server...
2020-10-13 23:03:39.221:INFO::main: Logging initialized @7360ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.12.100:60775/>. Press <Ctrl+C> to exit

Encouragement: [Tutorials that may help you]

These materials should be the most comprehensive and complete preparation warehouse for friends who do [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! Everything should be done as early as possible, especially in the technology industry, and the technical foundation must be improved.

Follow my WeChat public account [Program Yuanmuzi] to get it for free~
insert image description here

If you don't climb a mountain, you don't know how high the sky is. Waiting will only make you miss, and struggle will be successful.

insert image description here

My study group: 644956177 There are technical experts in the group to communicate and share~

Guess you like

Origin blog.csdn.net/Xsk215/article/details/117163066