Python test framework pytest (25) test report Allure - introduction, installation, examples, report structure

Keywords: pytest allure

1. Introduction

1. Allure framework is a flexible lightweight multi language test report tool. It not only shows the test content in the form of concise web report, but also allows everyone involved in the development process to extract the most useful information from the daily test execution.

2. From the perspective of dev/qa, the Allure report shortens the life cycle of common defects: test failures can be divided into bug and interrupted tests. Logs, steps, fixtures, accessories, timing, history and integration with TMS and bug tracking system can also be configured. Therefore, responsible developers and testers will have all the information.

3. From the perspective of managers, Allure provides a clear "big picture" of which features have been covered, where defects gather, what the implementation schedule looks like, and many other convenient things. The modularity and scalability of Allure ensure that you can always fine tune something to make Allure more suitable for you.

 

2. Installation

1, Command line installation:

Run the following command from the command line to install:

pip install allure-pytest

Or (using the domestic Douban source, the data will be synchronized with the foreign official website regularly, which is fast.)

pip install allure-pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2, Configure environment variables (optional):

Allure is a command line tool that can be downloaded and configured with environment variables.

Download address:

https://github.com/allure-framework/allure2/releases

 

1. Unzip the downloaded file.

 

 

2. Configure environment variables

Windows:

(1)Copy path, such as: D:\allure-2.13.6\bin
(2)environment variable->path Add environment edit->add to D:\allure-2.13.6\bin ->preservation

Mac:

(1)Copy path, such as:/Users/wangmeng/Documents/allure-2.13.6
(2)Command line input sudo vi ~/.bash_profile
(3)Add content, save and exit:
export ALLURE_HOME=/Users/wangmeng/Documents/allure-2.13.6
export PATH=$PATH:$ALLURE_HOME/bin
(4)Command line input (make environment variables effective) source ~/.bash_profile

 

3. Example

Project directory structure:

 

Confitest.py file in root directory

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest

@pytest.fixture(scope="session")
def login():
    print("===Log in and return to: name,token===")
    name = "AllTests"
    token = "123456qwe"
    yield name, token
    print("===sign out===")

Test under root directory_ Case.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest
from time import sleep

@pytest.mark.parametrize("n", list(range(5)))
def test_get_info(login, n):
    sleep(1)
    name, token = login
    print("===Get user personal information===", n)
    print(f"user name:{name}, token:{token}")

test_ Conf test.py file under Baidu package

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest

@pytest.fixture(scope="module")
def open_baidu(login):
    name, token = login
    print(f"===user {name} open baidu===")

test_ Test under Baidu package_ Case1.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest
from time import sleep

@pytest.mark.parametrize("n", list(range(5)))
def test_case1_1(open_baidu, n):
    sleep(1)
    print("===baidu Execute test cases test_case1_1===", n)

@pytest.mark.parametrize("n", list(range(5)))
def test_case1_2(open_baidu, n):
    sleep(1)
    print("===baidu Execute test cases test_case1_2===", n)

test_ Test in Weibo folder_ Case2.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest
from time import sleep

@pytest.mark.parametrize("n", list(range(5)))
def test_case2_no_fixture(login, n):
    sleep(1)
    print("===weibo No,__init__Test case test_case2_no_fixture===", login)

test_ conftest.py file under Douyin package

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest

@pytest.fixture(scope="function")
def open_douyin(login):
    name, token = login
    print(f"===user {name} open douyin===")

test_ Test under Douyin package_ Case3.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest
from time import sleep

@pytest.mark.parametrize("n", list(range(5)))
class TestDouyin:
    def test_case3_1(self, open_douyin, n):
        sleep(1)
        print("===douyin Execute test cases test_case3_1===", n)

    def test_case3_2(self, open_douyin, n):
        sleep(1)
        print("===douyin Execute test cases test_case3_2===", n)

Execute command:

Add the parameter -- allouredir option, and provide the folder path to the test results

For example:

pytest -n auto --alluredir=allure

Operation results:

Generate a lot of. json and. txt files under the specified allure folder

 

 

To view the test result report, you need to execute the allure command to generate the test report.

Execute command:

allure serve allure

 

After that, the default browser is automatically opened to display the test report.

 

 

4. Report structure  

  • Overview: overview.

  • Categories: category. By default, it is divided into failed and error. All execution results that are one of them will be classified into categories. You can quickly view which use cases are failed and error here.

  • Suites: test suite is the hierarchical relationship of all use cases. Use cases can be found according to package, module, class and method.

  • Graphs: graphical test results, including the distribution diagram of use case execution results, priority, time-consuming, etc.

  • Timeline: you can see the exact test timing (execution order) of the test case, including the execution time.

  • Behaviors: behavior driven. Test cases are grouped according to epic, feature and story.

  • Packages: test cases are grouped according to package and module.

 

The report can be switched between different languages (English by default)

 

 

View Suites (function level test cases)

Package name - module name - Test Case

 

 

View Suites (class level test cases)

Package name - module name - class name - Test Case

 

 

View test case details

Parameters: if @ pytest.mark.parameter is used, you can see what parameters and corresponding values are passed.

Set up: call the pre operation of fixture.

Tear down: call the post operation of the fixture.

stdout: output information.

 

 


 

 

Posted by mikanmao on Tue, 02 Nov 2021 05:48:17 -0700