Generate beautiful HTML graphical test reports with Pytest+allure

Source: Internet
Author: User
Tags testng

This article will show you how to use the Open Source test report to generate a framework allure build specifications, format uniform, beautiful test reports.
Through the introduction of this article, you will be able to:
-Combining the allure with the Pytest test framework;
-After performing the test, generate a test report in allure format.

1. Introduction of Allure Test Report

The allure is a very lightweight and very flexible open source test report generation framework. It supports most test frameworks, such as testng, Pytest, Juint, and so on. It's easy to use and easy to integrate. The following is a detailed description of how Pytest integrates with allure.

2. Pytest Frame Integrated Allure

Pytest is a Python unit testing framework that is easy and easy to use. It is highly recommended to use this test framework for small partners who test with Python, which is much more useful than the unittest that comes with Python. I'll cover the pytest test framework in a full article later. Today we mainly describe how to integrate the test report generation tool allure into pytest.

2.1 Installing Allure Pytest Adaptor

Allure pytest Adaptor is a plugin for pytest, through which we can generate the data needed to generate the test report for Allure. To install the Pytest-allure-adaptor plugin method:

    • 1
2.2 Using Allure Pytest adaptor to transform Pytest-based test cases

The features of Pytest-allure-adaptor are described in detail in the Pytest-allure-adaptor official website. This article will not be translated again, but from the actual start, to show you how to apply it to their own framework.
In order to generate reports using allure, you need to include the allure feature in the conftest.py and test scripts.

First, the conftest.py can output the information from the test environment to the report using the Allure.environment method, such as adding the host and test browser to the test report:

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22

Next, in the test script, add the Allure feature, and look directly at the script below, I explain the purpose of the allure feature by adding comments in the script. For example, the test script is test_shopping_trolley.py:

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

Several features of the allure are used:

    • @allure. Feature # is used to define the function being tested, the demand point of the product being measured
    • @allure. Story # A user scenario that defines the feature being tested, that is, a sub-function point
    • With Allure.step # is used to separate a test case into several steps in the report output
    • Allure.attach # used to enter some additional information into the test report, usually some test data information
    • @pytest. Allure.step # is used to output some common functions as test steps to the report, where this function is called to output steps in the report
3. Generate Allure Test Report

After the allure feature has been added to the test script, the test result data required by the Allure report is required for the test to be performed. When Py.test executes the test, specify the –ALLUREDIR option and the directory where the test data is saved:

    • 1

The result data for this test is saved in the./result/. Alternatively, you can execute a test case that specifies features or stories to perform part of the test case, such as the ' Add to cart ' sub-function under ' Shopping cart functionality ':

    • 1

Next we can generate the Allure test report using the test data generated by the above command. There are two ways to generate a Allure test report: One is the command line, and the other is the Jenkins plugin. The following are described separately.

3.1 Command-line 3.1.1 Installation command-line tools

First you need to install the command line tool, if it is a Mac computer, it is recommended to use homebrew installation.

    • 1
3.1.2 Generating test reports

After the installation is complete, generate the test report from the./result/directory using the following command:

    • 1

This generates the Allure test report in the./report/directory. –clean The goal is to empty the test report catalog before generating a new test report.

3.1.3 Open Test Report

Open the test report with the following command:

    • 1

The native browser will open the Http://127.0.0.1:8083/index.html Web page and display the test report.

Interpretation of 3.1.4 Test report

After you open the generated test report, the browser is automatically tuned to show the test report. Let's look at several pages of the test report separately.

    1. Home

      The first page shows the number of test cases for this test, the successful use cases, the failure cases, the ratio of skipped use cases, the test environment information, suites,features by stories and other basic information, when with Jenkins did the continuation of the trend area will also be shown, The passage of the previous tests.
      The left column of the home page also shows other information about the test report from different dimensions, so you can go inside and see for yourself.
    2. Behaviors
      Next, we click on the features by STORIES, which will go to the Behaviors page, which shows the results of the test cases according to features and STORIES:

      From this page you can see the "Shopping Cart features" features contains three stories test case execution.
    3. Suites
      The Allure test report will each test script as a suite. On the home page, click any of the suite below the suites area to go to the suites pages.

      This page shows the script's catalog results for all of the test case executions this time.

    4. Test Case Page
      Clicking on any of the test cases on the Suites page will show the details of the use case on the right side of the suites page.

      From this page, you can see each step of the test case execution and the results of each step execution. Why the test case failed, here at a glance.
3.2 Installing Allure Plugin with the Jenkins plug-in method 3.2.1

First you need to install Allure Plugin for Jenkins. On the Plugin Management page of Jenkins, search for "allure", on the Search results page, select "Allure Jenkins Plugin" to install.
Restart Jenkins after the installation is complete.
On the "Global Tools Management" page of Jenkins, locate the Allure commandline module to install:

Click Apply and save after clicking to pop up the following page, enter Allure's command alias and version.

3.2.2 Configuration Job

Follow the diagram below to write the corresponding configuration information in the Excute shell and post-build operations section.

This way, when our Jenkins job finishes executing the test case, we'll see the Allure report icon on the job's main page and click inside to see the allure reports.

4, Team practice recommendations

Different test frameworks may be used within the team, and test reports generated by each test framework will not be the same. However, since the Allure report supports a number of test frameworks, a consistent test report can be generated through allure, which facilitates the provision of a consistent test report externally.
Recommendation: The team internally uses allure to generate test reports.
Because of the java+testng test architecture of the projects within the team and the time behind it, I'll show you how to integrate allure in the TESTNG framework.

Generate beautiful HTML graphical test reports with Pytest+allure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.