pytest执行用例时,在conftest.py中写了失败截图的方法,因为之前框架用的pom模型,所以继承了basepage类。
执行过程中,发现conftest这个文件没有执行,用例执行失败时,没有截图并添加到allure,不知道时什么原因。下附目录结构和代码
- conftest.py
- from util.logger import logger
- import pytest
- import allure
- import os
- from util.basepage import Basepage
- class Screenshot(Basepage):
复制代码- # 用例失败后自动截图
- @pytest.hookimpl(tryfirst=True, hookwrapper=True)
- def pytest_runtest_makereport(self, item, call):
- """
- 获取用例执行结果的钩子函数
- :param item:
- :param call:
- :return:
- """
- outcome = yield
- report = outcome.get_result()
- if report.when == "call" and report.failed:
- mode = "a" if os.path.exists("failures") else "w"
- with open("failures", mode)as f:
- if "tmpir" in item.fixturenames:
- extra = " (%s)" % item.funcargs["tmpdir"]
- logger.info("------------success-----------")
- else:
- extra = ""
- f.write(report.nodeid + extra + "\n")
- with allure.step('添加失败截图...'):
- logger.info("-------------fail and screenshot----------")
- allure.attach(self.driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
复制代码basepage.py class Basepage(): - def __init__(self, driver):
- self.driver = driver
-
- def open_web(self, url):
- self.driver.get(url)
-
- def page_refresh(self):
- self.driver.refresh()
-
- def find_element(self, By, locator):
- elf = self.driver.find_element(By, locator)
- return elf
-
- def is_element_present(self, how, what):
- """判断当前页面中是否含有某元素"""
- try:
- self.driver.find_element(by=how, value=what)
- except NoSuchElementException as msg:
- print(msg)
- return False
- return True
-
- def find_elements(self, By, locator):
- els = self.driver.find_elements(By, locator)
- return els
-
- def attach_screenshot_allure(self):
- """[Allure报告中添加截图附件]
- """
- pic = self.driver.get_screenshot_as_png()
- # allure.attach(pic, "screenshot", allure.attachment_type.PNG)
- return pic
复制代码test_demo.py
import sys
sys.path.extend(['C:\demo'])
from util.logger import logger
from util.allurehelper import AllureHelper
import allure
from selenium import webdriver
from page_case.offline_order import offine_order_Page
from util.basepage import Basepage
import pytest
driver = webdriver.Chrome()
page = offine_order_Page(driver)
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
option.add_argument(r"user-data-dir=C:\python27\profile")
[url=]@allure.feature("线下订单")[/url]
class Testsuit: - def setup_class(self):
- # self.driver = webdriver.Chrome()
- # self.driver.implicitly_wait(5)
- # driver.implicitly_wait(5)
- # page = offine_order_Page(driver)
- page.pre_class()
- page.enter_statistic()
-
- def setup_method(self):
- page.pre_method()
-
- @allure.title("进入现金充值页面")
- @allure.severity("U")
- # @AllureHelper.attach_all_if_fail # 用例执行失败后操作
- def test_enter_cash_recharge_page(self):
- logger.info("-----进入现金充值页面------")
- page.enter_statistic()
- page.enter_cash_recharge_page()
- assert page.judge_enter_cash_recharge_page() == "现金充值1"
复制代码无报错,但是conftest文件没有执行,生成的allure报告中没有截图 求分析原因和解决方法
|