• pytest添加运行失败截图和使用自定义的css


    pytest添加运行失败截图

    在conftest.py中,定义截图的方法,失败时自动截图,并将截图保存到html页面中

    from common.base_driver import BaseDriver
    import pytest
    
    driver = None
    
    #失败自动截图,展示到html报告中
    @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="600px;height:300px;" ' 
                           'onclick="window.open(this.src)" align="right"/></div>' % screen_img
                    extra.append(pytest_html.extras.html(html))
            report.extra = extra
    
    
    #截图保存为base_64,展示在html中
    def _capture_screenshot():
        return driver.get_screenshot_as_base64()
    
    
    
    #定义公共的fixture
    @pytest.fixture
    def common_driver():
        global driver
        driver = BaseDriver().base_driver()
        yield driver
        driver.close_app()
        driver.quit()
    
    
    #定义含有toast弹框的fixture
    @pytest.fixture
    def common_toast_driver():
        global driver
        driver = BaseDriver().base_driver(automationName="UIAutomator2")
        yield driver
        driver.close_app()
        driver.quit()
    

    注意的是fixture修饰的方法中,不要忘了global driver

    运行之后显示测试报告的图片是宽度和高度过大,导致图片比较难看,因此我们需要优化下

    自定义css

    在html_reports目录下定义一个assets目录,在目录下将pytest-html插件源码中的style.css拷贝到assets下

    使用F12查看报告的图片的位置,是在class="extra"下的img目录中

    修改style.css,为了让这个样式优先级最高,加入!import

    .extra div div img {
    	 300px!important;
    	height: 500px!important;
    }
    

    修改main.py,使用--css, style.css路径--self-contained-html(这个表示将我们定义的css样式合并到html页面中)

    import pytest
    import time
    from common.conf_dir import html_reports_dir
    
    cur_time = time.strftime("%Y-%m-%d_%H-%M-%S")
    pytest.main([
            #"--reruns=1",
            #"--reruns-delay=10",
            "-m", "fail",
            "--junitxml", f"{html_reports_dir}/autotest_report_{cur_time}.xml",
            "--html", f"{html_reports_dir}/autotest_report_{cur_time}.html",
            "--css", f"{html_reports_dir}/assets/style.css",
            "--self-contained-html"]
    )
    

    最终效果

    这次报告看起来美观多了~

    参考文章

    《pytest文档8-html报告报错截图+失败重跑》
    《pytest 之 pytest-html生成html报告》

  • 相关阅读:
    MooseFS基础和安装
    自动化运维工具SaltStack详细部署
    linux中的通配符与正则表达式
    使用GitHub做个人博客
    mysql数据库分库备份脚本
    php-fpm配置详解
    php-fpm安装、配置与优化
    ceph的正常卸载与非正常卸载
    CentOS系统/tmp目录里面的文件默认保留多久
    ansible常用命令
  • 原文地址:https://www.cnblogs.com/my_captain/p/12728012.html
Copyright © 2020-2023  润新知