pytest generates html and allure test reports

pytest advanced html test report

1. pytest-html generates reports

pytest uses the pytest-html plug-in to generate test test reports, without writing the code to generate reports by yourself. github source address  https://github.com/pytest-dev/pytest-html

Install

Use the pip command -> pip install pytest-html like installing the python third-party library. I have already installed it here, so the output information you see may not be the same as mine

verify installation

Use pip list to check whether there is pytest-html, if there is, it means that it has been installed successfully

generate report

Use the command pytest --html=reportname.html (the parameter here is the name of the test report, note that there are two horizontal bars in front) let's try it!

Effect

It can be seen that this report is quite good, much more dazzling than the unittest report! Each case can be clicked to view the specific operation information! Well, you may find that the test report you generated is different from mine? Yes, that’s right, I have more description information here, but yours doesn’t seem to exist, so don’t worry.

Screenshot of error use case

Usually, we want to be able to capture a picture when a test case fails, so that we can know where the problem is or the approximate location of the problem, so that we can debug the code or analyze the bug of the software, so how to include it in this report What about inserting screenshots?

We mentioned the conftest.py file in the last article, so we can now put the code of the failed screenshot into this file (why put it here, I think students who have learned should know, if you don’t know, read the previous article), Look at the specific code!

import pytest
from selenium import webdriver
from py._xmlgen import html

_driver = None
# 测试失败时添加截图和测试用例描述(用例的注释信息)

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """当测试失败的时候,自动截图,展示到html报告中"""
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            screen_img = _capture_screenshot()
            if file_name:
                html = '<div><img src="https://img-blog.csdnimg.cn/2022010615435752451.png" alt="screenshot" style="width:600px;height:300px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % screen_img
                extra.append(pytest_html.extras.html(html))
        report.extra = extra

def _capture_screenshot():
    '''截图保存为base64'''
    return _driver.get_screenshot_as_base64()

@pytest.fixture(scope='module')
def driver():
    global _driver
    print('------------open browser------------')
    _driver = webdriver.Firefox()

    yield _driver
    print('------------close browser------------')
    _driver.quit()

 Note that this module is from py._xmlgen import html. Most of us see on the Internet that use from py.xml import html. When I use this module, I will report an error, so I replaced it with this module (it is estimated to be related to the python version. I use 3, 3 is estimated not to have this module).

Ok, now let's execute our test case

That's right, the failed use case has a picture, and it's very clear. My use case asserts that the error message when the login fails is correct (for demonstration, I changed the expected value in the use case), the actual prompt message is 'account and password error' and 'please enter the account number', but what we expect is' The account and password are wrong' and 'please enter the account', it is obvious that our software has bugs, and it is clear at a glance! Is it very practical... Next, let's take a look at how to add description information.

add description

Under normal circumstances, our report does not have the item of description information, so we can only implement it by adding code (the description information is the annotation information above our use case function). According to the official documentation, we know that the report table can be added and deleted

We add such a line of code after the above code report.extra = extra

report.description = str(item.function.__doc__)

and add these two methods in the file

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))

 We can try to run our test case again to see if the report should be the same as mine.

summary

You can write the code and try it yourself to see if you will get the report we want. In fact, this article does not explain the code writing process in detail, nor does it explain in detail which piece of code is used for what, because these codes are a The template is also universal, so you can save it, and you can use it wherever you write the project in the future (as a python programmer, the ultimate idea is to simplify if you can, write code simply if you can, and copy Just copy, well, that's it!)

2. Allure generates reports

This is amazing. I personally think this tool is really awesome, and the generated reports are also super cool. In fact, it is a report framework that supports various test frameworks in various languages, and can be integrated into jenkins for use

install allure

The json to html tool generated by Allure installs
the mac version

1.: brew install allure
2. Enter the upper-level report directory and execute the command: allure generate report/ -o report/html
3. An index.html file will be generated in the report directory, which is a visual report

windows version 

1. Download the compressed package allure-2.7.0.zip
 address: https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.7.0/allure-2.7.0.zip
2. Unzip
3. Configure the bin directory in the compressed package to the path system environment variable
4. Enter the upper-level report directory and execute the command: allure generate report/ -o report/html --clean

Download the latest version from github https://github.com/allure-framework/allure2/releases

decompress

After downloading, extract it to the root directory of our project, which is the directory where the pytest code needs to be run. Below is my project root directory

modify by linux超 at 2019.08.30

Can be unzipped to any directory

install plugin

pip install allure-pytest

Note: When pytest-allure-adaptor and allure-pytest exist at the same time, an error will be reported when running, just uninstall pytest-allure-adaptor

Generate xml format report

Execute pytest -s -q --alluredir ./report in the directory where we need to run the use case (the report folder in the current directory, the name of this folder, you can specify the location yourself, if not specified, it will be generated in the current directory by default)

Add environment variables

Add the allure-2.10.0\bin directory to the environment variable. Of course, you can not add it. The purpose of adding the environment variable is to execute the allure.bat script in the bin directory anywhere

Run allure to generate report

We have already generated a report in xml format through this command pytest -s -q --alluredir report and saved it under report. Then we execute the command allure generate report/ -o report/html. The first report is the specified previous xml The directory of the report, the following report can also specify which directory to generate the final html report, we find our report according to the following path, and see the effect of the generated report

modify by linux超 at 2019.08.30

Note: The report directory cannot be the same, an empty directory must be specified to generate the final html report

Effect

This report looks really cool, and the menu on the left can be clicked to open, which records the execution of our use cases, so let's try it!

Summarize

main content

1. The pytest-html plug-in generates a test report, and how to capture a picture and add it to the report when the use case fails, and how to add the use case description to the report

2. How to use allure to generate a test report

The above two methods of generating test reports hope to help you and apply these skills to our actual work!

This article refers to the blog posts of the big guys on the Internet. I am just doing a porting summary here.

Guess you like

Origin blog.csdn.net/Jiazengzeng/article/details/116149023