在 with 中使用函数

问题描述

Python unittest.Testcase 类具有一系列检查函数包括检查断言是否被提出的函数

311 -    def assertRaises(self,excclass,callableObj,*args,**kwargs): 
319          try: 
320              callableObj(*args,**kwargs) 
321          except excclass: 
322              return 
323          else: 
324              if hasattr(excclass,'__name__'): excName = excclass.__name__ 
325              else: excName = str(excclass) 
326              raise self.failureException,"%s not raised" % excName 

用户扩展 TestCase 并像这样使用上面的内容

with self.AssertRaises(ValueError):
   x = 5/"3"

如果你引发ValueError

,你会引发一个错误

我想从 TestCase 中取出这个函数并以同样的方式在我的测试脚本中使用它,但我不知道如何使 assertRaises 函数在 { {1}} 语句。

如何进行这种转变?我在 with source code 中没有看到任何设置上下文的内容。没有 TestCase__enter__ 函数__exit__ 不扩展任何东西。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)