【发布时间】:2021-04-20 22:57:26
【问题描述】:
目前我正在使用以下解决方案在 PyTest 的每个测试函数结束时截取屏幕截图。如何确保仅在测试失败时才截取屏幕截图?这是一个关于 PyTest 机制的问题。这个问题与 selenium 或 appium 无关。
我在 Stackoverflow 上发现了类似的问题,但并不完全相同。为其他问题提供的解决方案无法回答我的问题。由于使用 PyTest 对测试失败进行截图是一个常见问题,因此我认为它应该得到一个单独且非常具体的答案。
@pytest.fixture(scope="function", autouse=True)
def take_screenshot(self, appium_driver):
yield
time.sleep(1)
current_filename_clean = os.path.basename(__file__).replace("test_", "").replace(".py", "")
current_test_name = os.environ.get("PYTEST_CURRENT_TEST").split(":")[-1].split(" ")[0].replace("test_", "")
appium_driver.get_screenshot_as_file(
f'test_reports/{current_filename_clean}_android_{current_test_name}_{datetime.today().strftime("%Y-%m-%d")}.png')
【问题讨论】:
-
这是一个相当流行的话题,在文档中多次提及。夹具示例:Making test result information available in fixtures,钩子示例:post-process test reports / failures.
-
我找到了那些文章,但是,对于我的问题,那里的解决方案并不那么简单。我相信,由于这是一个相当普遍的问题,最好为它提供一个具体而切题的答案和最佳实践目录。