仅在测试失败时创建屏幕截图 [英] Create screenshot only on test failure

查看:170
本文介绍了仅在测试失败时创建屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个可以创建屏幕截图的函数,在这里称为

Currently I have a function that creates a screenshot and I call it here

def tearDown(self):
    self.save_screenshot()
    self.driver.quit()

还有一个正在创建的文件夹,用于存储屏幕截图.

There is also a folder being created which is used to store the screenshots.

我不希望这种情况在测试通过时发生.

I don't want this to happen when the test passes.

我必须添加些什么才能使这种情况不发生?

What do I have to add in order for this not to happen?

感谢所有帮助

推荐答案

这是仅在失败时捕获屏幕截图的一种方法:

Here is one way to capture screenshot only when failure:

def setUp(self):
    # Assume test will fail
    self.test_failed = True

def tearDown(self):
    if self.test_failed:
        self.save_screenshot()

def test_something(self):
    # do some tests
    # Last line of the test:
    self.test_failed = False

此方法的基本原理是,当测试到达最后一行时,我们知道测试已通过(例如,所有self.assert *通过).此时,我们重置test_failed成员,该成员在setUp中设置为True.在tearDown中,我们现在可以判断测试是否通过或失败,并在适当时进行截图.

The rationale behind this approach is when the test reaches the last line, we know that the test passed (e.g. all the self.assert* passed). At this point, we reset the test_failed member, which was set to True in the setUp. In tearDown, we now can tell if a test passed or failed and take screenshot when appropriate.

这篇关于仅在测试失败时创建屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆