我正在使用Python + pytest +硒运行ui测试。 我需要在任何测试失败(任何异常/断言错误等)上做一个屏幕截图
我想在我的BaseEnvironment类中实现它,现在看起来像这样
@pytest.mark.flaky(max_runs=3, min_passes=1)
class Rerun:
pass
class BaseEnvironment(Rerun):
@classmethod
def setup_class(cls):
warnings.simplefilter("ignore")
cls.driver = create_webdriver()
@classmethod
def teardown_class(cls):
cls.driver.close()
cls.driver.quit()
我在google上找到了很多解决方案,但是对于pyrhon 2.x来说确实很复杂。我需要一些简单的东西,例如装饰器。它必须适用于我所有的测试。 但是,在每次测试结束时 test_status = False 并添加到设置 test_status = False 中,并进行删除即可。如果可能的话,我想清洁它。 感谢您的建议!
答案 0 :(得分:1)
我也希望做到这一点,上面的答案是一个好的开始,但最终我使用了一些不同的东西:
def screenshot_dec(func):
def wrapper(selenium):
try:
func(selenium)
except:
selenium.save_screenshot("error_{0}.png".format(func.__name__))
raise
return wrapper
@screenshot_dec
def test_my_test(selenium):
# test code here where selenium is my driver instance
这会在发生任何异常时生成error_test_mytest.png
。
答案 1 :(得分:0)
这里是一个装饰器,应该在每次异常和失败的断言之后拍摄屏幕截图,我相信这是您想要的。尝试类似以下的内容吗?
def decorator_screenshot(func):
def wrapper(func, *args, **kwargs):
try:
return func(*args, **kwargs)
except:
return get_screenshot()
return wrapper
@decorator_screenshot
def test_something():
Assert.fail("failed test")