Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python在tearDown()方法中拍摄测试失败的屏幕截图_Python_Automated Tests_Screenshot_Teardown - Fatal编程技术网

python在tearDown()方法中拍摄测试失败的屏幕截图

python在tearDown()方法中拍摄测试失败的屏幕截图,python,automated-tests,screenshot,teardown,Python,Automated Tests,Screenshot,Teardown,我正试图在一次失败的测试结束时截图 我做了一些有效的事情,但有一个问题: 它会截取失败测试后发生的每个测试的屏幕截图,例如: 1.测试通过:没有截图 2.测试失败:屏幕截图 3.测试通过:截图 -所以在第一次出错后,所有测试都会得到一个屏幕截图 我知道这是_resultForDoCleanups方法的一个问题,在我的例子中不能用_outcomeForDoCleanups.success替换,因为我使用的是python 2.7,而不是3 我的代码: def tearDown(self):

我正试图在一次失败的测试结束时截图

我做了一些有效的事情,但有一个问题: 它会截取失败测试后发生的每个测试的屏幕截图,例如: 1.测试通过:没有截图 2.测试失败:屏幕截图 3.测试通过:截图 -所以在第一次出错后,所有测试都会得到一个屏幕截图

我知道这是_resultForDoCleanups方法的一个问题,在我的例子中不能用_outcomeForDoCleanups.success替换,因为我使用的是python 2.7,而不是3

我的代码:

    def tearDown(self):
            if self._test_has_failed():
                if not os.path.exists(SCREEN_DUMP_LOCATION):
                    os.makedirs(SCREEN_DUMP_LOCATION)
                for ix, handle in enumerate(self.driver.window_handles):
                    self._windowid = ix
                    self.driver.switch_to.window(handle)
                    self.take_screenshot()
            self.driver.quit()

def _test_has_failed(self):
        for method, error in self._resultForDoCleanups.errors:
            if error:
                return True
        return False

def _get_filename(self):
    timestamp = datetime.now().isoformat().replace(':', '.')[:19]
    return "{folder}/{classname}.{method}-window{windowid}-{timestamp}".format(
        folder=SCREEN_DUMP_LOCATION,
        classname=self.__class__.__name__,
        method=self._testMethodName,
        windowid=self._windowid,
        timestamp=timestamp
    )

def take_screenshot(self):
    filename = self._get_filename() + ".png"
    print "\n{method} SCREENSHOT AND HTML:\n".format(
        method=self._testMethodName)
    print 'screenshot:', filename
    self.driver.get_screenshot_as_file(filename)

您的代码非常接近您想要的结果

此代码使用@jornsharpe的注释

def tally(self):
    return len(self._resultForDoCleanups.errors) + len(self._resultForDoCleanups.failures)

def setUp(self):
    self.errors_and_failures = self.tally()

def tearDown(self):
    if self.tally() > self.errors_and_failures:
        # Take a screenshot
在每个测试方法开始时,我们会找出我们有多少错误和失败。当我们使用tearDown方法时,我们的测试方法已经执行,我们将知道是否有错误。Tall方法将比我们在测试方法之前设置的self.errors_和_failures变量高1


希望这就是您所寻找的。

那么您想检查是否只有您刚刚测试的
方法存在
错误
?一种稍微有点不太正常的方法是查看是否有比上次调用时更多的错误。是的,对于unittest类中的测试用例,为什么不
记录
?屏幕截图的存储成本很高,而且对于进行后期调试的人来说几乎毫无用处。@jedrekcienicki在stackexchange上您应该避免使用“谢谢”评论,但您可以将答案标记为正确:)