How to generate test reports using allure in python automation

The allure test report framework helps you easily realize the "tall up" report display. This article demonstrates how to integrate the allure test framework from 0 to 1 through examples. It focuses on how to integrate allure into the existing automatic test project and how to optimize the display of reports. Allure is very powerful and supports many
Related topics: javapython www cppcns. comnbsp; (if the Homebrew version is old, otherwise the installed allure version is also very old, you need to upgrade Homebrew first
The allure test report framework helps you easily realize the "tall up" report display. This article demonstrates how to integrate the allure test framework from 0 to 1 through examples. It focuses on how to integrate allure into the existing automatic test project and how to optimize the display of reports. Allure is very powerful and supports multiple languages and multiple testing frameworks, whether java/python or Junit/TestNG. The implementation process of other languages or frameworks is consistent with this article. Refer to the specification of each language framework for specific configuration

install

Install allure

Windows users:

Scoop install allow (you need to download and install scoop first. This method does not need to configure environment variables)
MAC user:

Automatic installation via Homebrew
brew install allure &www.cppcns. comnbsp; (if the Homebrew version is relatively old, you need to upgrade Homebrew first, otherwise the installed allure version is also very old and may be incompatible with the Python plug-in)
Manual installation:

From the official website https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/ Manual Download
The latest version is 2.13.6
How to generate test reports using allure in python automation

After downloading, unzip and configure environment variables

Install the allure pytest plug-in

pip install allure-pytest

allure common features

You can use @ feature,@story,@step,@attach if you want to see the test function, sub function or scenario, test steps and additional test information in the report

Steps:

import allure
Add @ allure Feature ("function name")
Add @ allure. To the subfunction Story ("subfunction name")
Add @ allure. To the step Step ("step details")
@allure.attach("specific text information"), the information that needs to be attached can be data, text, pictures, videos and web pages
If only some functions are tested, restriction filtering can be added when running:
pytest file name -- allure features "name of function to be run"

allure feature - feature/story

@allure.feature and @ allure Store relationship

Feature is equivalent to a function, a large module, which classifies case s into a feature, and the report is displayed in behavior, which is equivalent to testsuite
story is equivalent to different scenes and branch functions under this function or module. It belongs to the structure under feature. The report is displayed in features, which is equivalent to testcase
feature and story are similar to parent-child relationship
step characteristics

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

Under test www.cppcns.com Collect results during trial execution

pytest [test file] - S - Q -- allouredir =/ result --clean-alluredir

– allouredir this option is used to specify the path to store the test results
– clean allouredir this option is used to clear the previously generated results
View test report:

Method 1: after the test is completed, view the actual report and view the report online. The default browser will be directly opened to display the current report

allure serve ./result

Method 2: generate a report from the results. This is a service to start tomcat, which requires two steps

Generate report:

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

Open report:

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

for instance:

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('Login module')
class TestLogin():
    @allure.story('Login successful')
    @allure.title('Login success title')
    def test_login_sucess(self):
        with allure.step('Step 1: open the application'):
            print('App opened')
        with allure.step('Step 2: enter the login page'):
            print('The login page is open')
        with allure.step('Step 3: enter user name and password'):
            print('User name and password entered successfully')
        print('Login test case: login succeeded')

    @allure.story('Login successful')
    def test_login_sucess2(self):
        assert '1' == 1
        print('Login test case: login succeeded')

    @allure.story('Login failed')
    def test_login_failure_a(self):
        print('Login test case: login failure, missing user name')

    @allure.story('Login failed')
    def test_login_failure_b(self):
        print('Login test case: login failed, password missing')

    @allure.story('Login failed')
    def test_login_failure_c(self):
        with allure.step('enter one user name'):
            print('User name entered')
        with allure.step('Input password'):
            print('Password entered')
        with allure.step('Click login'):
            print('Click login')
        print('Login test case: login failure, password error')


@allure.feature('Search module')
class TestSearch():
    def test_search1(self):
        print('Search case 1')

    TEST_CASE_LINK = 'https://mirrors.huaweicloud.com/'
    @allure.testcase(TEST_CASE_LINK,'Test case connection')
    def test_search2(self):
        print('Search case 2')
    @allure.step('Search steps')
    def test_search3(self):
        print('Use case search 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('Login successful')
    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

Report generated:

How to generate test reports using allure in python automation

allure feature - testcase

Associate 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,'Test case connection')
def test_search(self):
    print('Search case')

How to generate test reports using allure in python automation

Conduct a range of tests according to the importance level

Generally, the tests include P0, smoke test and verification on-line test. If it is implemented according to the importance level, for example, the main process and important modules should be run again when going online, which can be solved by the following methods

By attaching @ pytest Mark mark

Via allure feature,allure. story

You can also use allure Severity to attach tags

Level:
trivial: unimportant, minor defect (no prompt for required items or nonstandard prompt)
Minor is not very important, minor defect (interface error is inconsistent with UI requirements)
Normal: normal problem, common defect (numerical calculation error)
Critical: serious, critical defect (missing function points)
blocker: blocking, interrupt defect (the client program is unresponsive and cannot perform the next operation)
usage method:

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

Execution:

pytest -s -v file name -- 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


# Without any mark, the default is normal
def test_with_no_severity():
    pass


# trivial: unimportant, minor defect (no prompt for required items or nonstandard prompt)
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass


# Minor level is not very important, minor defect (interface error is inconsistent with UI requirements)
@allure.severity(allure.severity_level.MINOR)
def test_with_minor_severity():
    pass


# Normal: normal problem, common defect (numerical calculation error)
@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass


# Critical: serious, critical defect (missing function points)
@allure.severity(allure.severity_level.CRITICAL)
def test_with_ritical_severity():
    pass


# blocker: blocking, interrupt defect (the client program is unresponsive and cannot perform the next operation)
@allure.severity(allure.severity_level.BLOCKER)
def test_with_blocker_severity():
    pass


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

    # Without any mark, it defaults to the same class level
    def test_inside_with_normal_severity(self):
        pass

    # The critical level has been reset
    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_with_critical_severity(self):
        pass

Execution:

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 How to use automation allure Generate test report

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("Baidu search")
@pytest.mark.parametrize('test_data1',['allure','pytest','unittest'])
def test_steps_demo(test_data1):
    with allure.step('Open Baidu web page'):
        driver=webdriver.Chrome()
        driver.get('http://www.baidu.com')
        driver.maximize_window()
    with allure.step(f'Enter search term:{test_data1}'):
        driver.find_element_by_id('kw').send_keys(test_data1)
        time.sl Programming Inn eep(2)
        driver.find_element_by_id('su').click()
        time.sleep(2)
    with allure.step('Save picture'):
        driver.save_screenshot('./screenshot/baidu.png')
        allure.attach.file('./screenshot/baidu.png',attachment_type=allure.attachment_type.PNG)
    with allure.step('Close browser'):
        driver.quit()

Execution:

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

Mutual encouragement: [tutorials that may help you]

These materials should be the most comprehensive and complete war preparation warehouse for friends doing [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 soon as possible, especially in the technology industry. We must improve our technical skills.

Pay attention to my WeChat official account [program Muzi] free access.

If you don't climb the mountain, you don't know the height of the sky. Waiting will only miss, and struggle can succeed.

My learning group: 644956177. There are technical cows in the group to communicate and share~

Keywords: Python software testing Testing

Added by gareh on Wed, 09 Feb 2022 02:13:06 +0200