pytest-allure报告添加错误截图

方法一:

basePage.py

    def get_picture(self):
        """
        获取错误截图
        :return:
        """
        str1 = strftime("%Y-%m-%d %H-%M-%S", localtime(time()))
        file_name = str1 + ".png"
        path = os.path.dirname(os.path.abspath(".")) + "\\screenshots\\" + file_name
        return path

test_lesson.py

class Test_Lesson(object):

    @pytest.fixture(scope="function", autouse=True)
    def setup_class(self, browser):
        self.lPage = LessonPage(browser)
        self.lPage.implicitly(10)
        self.file_path = self.lPage.get_picture()


    def test_1(self, browser):
        try:
            assert 1 == 2
        except Exception as e:
            self.lPage.save_screenshot("错误截图", self.file_path)
            raise e

 

方法二:

conftest.py文件

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """
    获取每个用例的钩子函数
    :param item:
    :param call:
    :return:
    """
    outcome = yield
    rep = outcome.get_result()
    if rep.when == "call" and rep.failed:
        mode = "a" if os.path.exists("failures") else "w"
        with open("failures", mode) as f:
            if "tmpdir" in item.fixturenames:
                extra = " (%s) " % item.funcargs["tmpdir"]
            else:
                extra = ""
            f.write(rep.nodeid + extra + "\n")
        item.name = item.name.encode("utf-8").decode("unicode-escape")
        file_name = item.name + ".png"
        path = os.path.dirname(os.path.abspath(".")) + "\\screenshots\\" + file_name
        driver.save_screenshot(path)

        if hasattr(driver, "get_screenshot_as_png"):
            with allure.step("添加失败截图"):
                allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)

 https://www.cnblogs.com/yoyoketang/p/12609871.html

posted @ 2021-04-19 17:05  海浪。  阅读(724)  评论(0)    收藏  举报