app自动化测试框架(二、测试报告)

2021-08-03  本文已影响0人  HC2

一、
安装:

pip3 install pytest-html

main.py:

path = cur_path + '/web/autotest/ui/'
now = time.strftime("%Y-%m-%d")
xml_file =  path + 'result.xml'
report_file =  path + now +'result.html'

pytest.main([
    'test_suites/test_login.py',
    "--html=%s" % (report_file),  #生成html测试报告
    "--junitxml=%s"%(xml_file)
])

image.png

二、#通过conftest来实现报告的描述

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__))

@pytest.fixture()
def app_page():
    logging.info('----------------测试开始-----------------')
    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '8.1.0'
    desired_caps['deviceName'] = 'xxxx'
    desired_caps['appPackage'] = 'xxxx'
    desired_caps['appActivity'] = 'xxxxx'
    desired_caps['autoGrantPermissions'] = True
    desired_caps['automationName'] = 'uiautomator2'

    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    yield driver
    logging.info('----------------测试结束-----------------')
    driver.quit()


二、通过conftest来实现报告的描述
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))  #html报告中插入一列,列头名为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__)

测试用例备注测试描述:

import pytest
import logging
from page_obj.login_page import LoginPage

class TestLogin:

    @pytest.mark.smoke
    def test_login_success(self, app_page):
        """成功登录""" #这是测试描述备:
        # 步骤
        LoginPage(app_page).login('xxxxxxx', 'xxxxxx')
        logging.info("开始断言")
image.png

三、错误用例保存截图

conftest.py

@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" alt="screenshot" style="width:375px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % screen_img
                extra.append(pytest_html.extras.html(html))
        report.extra = extra
image.png
上一篇 下一篇

猜你喜欢

热点阅读