这是我的密码。在
import unittest
import warnings
def function_that_raises_CustomWarning():
warnings.warn("warning")
return True
class test(unittest.TestCase):
def test(self):
is_this_True = False
is_CustomWarning_raised = False
try:
is_this_True = function_that_raises_CustomWarning()
except Warning:
is_CustomWarning_raised = True
self.assertTrue(is_this_True)
self.assertTrue(is_CustomWarning_raised)
if __name__ == "__main__":
unittest.main()
self.assertTrue(is_this_True)
中的is_this_True
是{
我想要的是self.assertTrue(is_this_True)
中的is_this_True
是{function_that_raises_CustomWarning()
中发出警告之后返回的。在
如何返回function_that_raises_CustomWarning()
中的值,同时“捕获”了except
中的警告?在
当我在Windows上用3.6运行代码时,失败的是}。我用后者来说明如何使用它来添加额外的测试。在
self.assertTrue(is_CustomWarning_raised)
。默认情况下,警告不是异常,不能用except:
捕获。解决方案是使用assertWarns
或{相关问题 更多 >
编程相关推荐