pytest+selenium+allure+jenkins UI自动化实现用例失败自动截图原创
金蝶云社区-廖彬植
廖彬植
66人赞赏了该文章 2,214次浏览 未经作者许可,禁止转载编辑于2020年12月21日 09:32:23

UI自动化能够代替重复的人工回归测试任务,一个能够提供帮助的UI自动化框架,便捷定位问题的功能必不可少。有两方面能够方便对失败用例进行分析定位,一个是日志记录,另一个是失败截图。精准的截图能够让我们快速了解到用例运行到哪个页面后执行失败,以下是UI自动化使用allure自动化报告实现用例失败自动截图的简介(主要介绍关键点,相关框架的详细细节后续更新,如有帮助,请多多点赞【emoji】)。

1、在conftest.py文件中加入以下代码

image.png

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item):
    """
    hook pytest allure用例失败截图
    :param item:
    :return:
    """
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()
    # we only look at actual failing test calls, not setup/teardown
    if rep.when == "call" and rep.failed:
        mode = "a" if os.path.exists("failures") else "w"
        with open("failures", mode) as f:
            # let's also access a fixture for the fun of it
            if "tmpdir" in item.fixturenames:
                extra = " (%s)" % item.funcargs["tmpdir"]
            else:
                extra = ""
            f.write(rep.nodeid + extra + "\n")
        # pic_info = adb_screen_shot()
        with allure.step('查看失败截图'):
            allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)


2、使用allure测试报告,运行命令如下

if __name__ == "__main__":
    report_path = os.path.dirname(os.path.abspath('.')) + '/report/'
    command = ['-q', '-s', 'test_certificate_manage.py',
               '--log-format=%(asctime)s %(filename)s(%(lineno)d) %(levelname)s 日志:%(message)s',
               '--log-date-format=%Y-%m-%d %H:%M:%S',
               '--alluredir={report_path}test_certificate_manage'.format(report_path=report_path)]
    pytest.main(command)


3、运行用例,当用例失败时会自动对当前失败页面进行截图,会展示在对应用例报告中,以下是集成在jenkins上的效果

image.png



image.png


单击可放大图片


image.png



图标赞 66
66人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!

您的鼓励与嘉奖将成为创作者们前进的动力,如果觉得本文还不错,可以给予作者创作打赏哦!

请选择打赏金币数 *

10金币20金币30金币40金币50金币60金币
可用金币: 0