python2.6:
with self.assertRaises(SystemExit, func) as cm:
your_method()
self.assertEqual(cm.exception.error_code, 1)
>python 2.7:
with self.assertRaisesRegexp(SystemExit, '1'):
your_method()
本文对比了Python 2.6和Python 2.7中使用assert方法的不同之处,特别是通过自定义异常和正则表达式进行异常处理的实现方式。
python2.6:
with self.assertRaises(SystemExit, func) as cm:
your_method()
self.assertEqual(cm.exception.error_code, 1)
>python 2.7:
with self.assertRaisesRegexp(SystemExit, '1'):
your_method()
被折叠的 条评论
为什么被折叠?