看到 java 可以用监听器来实现,原理还不是很懂。
关于 python 我没找着好点的方式,希望大神指点!!!
下面列出比较拙略的两种方式
(感谢嵇 XX)
# encoding:utf-8
'''
Created on 2016年12月07日
@author: xiaomeizi
'''
import unittest
from xmz.ylb.utils.ScreenShot import screenshot
from xmz.ylb.utils.Log import logger
class test(unittest.TestCase):
"""
方式一:
用if语句来判断对比值,如果false,调用截图功能
"""
def test_a(self):
if not (1==2):
screenshot()
# logger.debug('This is debug message')
self.assertEqual(parameterOne, parameterTwo)
"""
方式二:
使用try ……except……finally
如果false,finall再断言一次
如果true,再次断言也无影响
"""
def test_b(self):
self.assertFuc(1, 2)
"""
断言
断言错误后截图、打log
"""
def assertFuc(self,parameterOne,parameterTwo):
try:
self.assertEqual(parameterOne, parameterTwo)
except AssertionError,e:
screenshot()
# logger.error(e)
print e
finally:
self.assertEqual(parameterOne, parameterTwo)
if __name__ =='__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(test)
unittest.TextTestRunner(verbosity=2).run(suite)
addError(test, err)
Called when the test case test raises an unexpected exception. err is a tuple of the form returned by sys.exc_info(): (type, value, traceback).
The default implementation appends a tuple (test, formatted_err) to the instance’s errors attribute, where formatted_err is a formatted traceback derived from err.
直接看图
另外 assertFuc 更正下~~~
def assertFuc(self,parameterOne,parameterTwo):
try:
self.assertEqual(parameterOne, parameterTwo)
except AssertionError,e:
screenshot()
# logger.error(e)
print e
raise