App automation test framework (III. allure beautification test report)

Time:2022-5-8

1、 Install allure
allure
file:
https://docs.qameta.io/allure/#

Allure download address:
https://repo1.maven.org/maven2/io/qameta/allure/allure-commandline/2.13.8/

Allure installation
(1) You need to install the Java environment first
(2) Windows downloads the zip package, unzip it and add the bin directory to the environment variable
Mac can be installed with brew

brew install allure

(3) Install allure pytest Library

pip install allure-pytest

Reprint:https://blog.csdn.net/z_erduo/article/details/116332523?spm=1001.2014.3001.5502

2、 Production report
main.py

import pytest,os
cur_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
result_path = cur_path + '/web/autotest/ui/result'
result_port = cur_path + '/web/autotest/ui/result_port'
if not os.path.exists(result_path):os.makedirs(result_path)
if not os.path.exists(result_port):os.makedirs(result_port)

pytest.main([
    '--alluredir',
    result_path,
    'test_suites/test_login.py',
])
os. System ("allow generate -- clean" + result_path + "-- report dir" + result_port) # convert to HTML

Result after successful execution_ The report file will be generated under path

image.png

result_ Report generates an HTML report file

image.png

Open report by:

Method 1: Execute command:
allure serve result_ Path path
The browser automatically opens the report

Method 2: enter result_ Report path
Open result with pycharm_ Report file, open index HTML browser

PS: if you directly right-click the folder to open index HTML failed to generate report

image.png

3、 Enhanced reporting visibility:
Allure properties
See the test function, sub function, or scenario in the report, and the test steps include test additional information

Usage parameter value parameter description
@allure. Epic () epic description
@allure. Feature () module name
@allure. Story() sub module name
@allure. Title () the title of the use case
@allure. Testcase() the link address of the test case
@allure. The issue () defect corresponds to the link in the defect management system
@allure. Description () description of test case
@allure. Step () operation steps steps of test cases
@allure. Severity() use case level blocker, critical, normal, minor, trivial
@allure. Link () link defines a link, which is displayed in the test report
@allure. Attachment() report add attachments

1. Add login block distinction and sub use case title to the login module

import pytest
import logging
from page_obj.login_page import LoginPage
import allure

@allure. Feature ("login module")
class TestLogin:

    @allure. Title ("successful login")
    def test_login_success(self, app_page):
        LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
        logging. Info ("start assertion")


    @allure. Title ("failed login")
    def test_login_error(self, app_page):
        LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
        logging. Info ("start assertion")
image.png

2、@allure.story

import pytest
import logging
from page_obj.login_page import LoginPage
import allure

@allure. Feature ("login module")
class TestLogin:

    @allure. Story ("mobile login")
    @allure. Title ("successful login")
    def test_login_success(self, app_page):
        LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
        logging. Info ("start assertion")

    @allure. Story ("mobile login")
    @allure. Title ("failed login")
    def test_login_error(self, app_page):
        LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
        logging. Info ("start assertion")


    @allure. Story ("account password login 1")
    @allure. Title ("successful login")
    def test_login_error2(self, app_page):
        LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
        logging. Info ("start assertion")
image.png

3. Add test step

import pytest
import logging
from page_obj.login_page import LoginPage
import allure

@allure. Feature ("add address")
class Testaddress:

    @allure. Title ("address added successfully")
    def test_address_success(self, app_page):
        with allure. Step ("step 1: login user"):
            Print ("login user")

        with allure. Step ("step 2: delete address"):
            Print ("delete address")

        with allure. Step ("Step3: add address"):
            Print ("add address")

        with allure. Step ("Step4: assertion"):
            assert 1==1
image.png

4. Add failed screenshot

conftest.py

import pytest, os
from selenium import webdriver
from appium import webdriver
import logging
from py._xmlgen import html
cur_path = os.path.dirname(os.path.realpath(__file__))
import allure
driver = None

@pytest.fixture()
def app_page():
    logging. Info ('------------------ test start -----------------')
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.1.0'
    desired_caps['deviceName'] = 'c6e989d7'
    desired_caps['appPackage'] = 'xxxx'
    desired_caps['appActivity'] = 'xxxxx'
    desired_caps['autoGrantPermissions'] = True
    desired_caps['automationName'] = 'uiautomator2'

    global driver
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    yield driver
    logging. Info ('----------------- end of test ------------')
    driver.quit()


#The description of the report is realized through conf test
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells. Insert (1, HTML. Th ('description ') #html report inserts a column with the column header name description

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

# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item, call):
    # outcome = yield
    # report = outcome.get_result()
    # report.description = str(item.function.__doc__)

# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item):
#     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="data:image/png;base64,%s" style="width:375px;" ' \
#                        'onclick="window.open(this.src)" align="right"/></div>' % screen_img
#                 extra.append(pytest_html.extras.html(html))
#         report.extra = extra

# def _capture_screenshot():
#'' 'save screenshot as Base64' ''
#     return driver.get_screenshot_as_base64()


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    '''
    Hook function to get the status of each use case
    :param item:
    :param call:
    :return:
    '''
    #Get the call result of hook method
    outcome = yield
    rep = outcome.get_result()
    #Only get the case call and the execution result is failure, excluding setup / teardown
    if rep.when == "call" and rep.failed:
        #Add screenshot of allure Report
        if hasattr(driver, "get_screenshot_as_png"):
            with allure. Failed to add step (.. '):
                allure. Attach (driver. Get_screenshot_as_png(), "failed screenshot", allure attachment_ type. PNG)
image.png