python - 在 with 中使用函数
问题描述
Pythonunittest.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
函数在with
语句中工作。
如何进行这种转变?我在TestCase
源代码中看不到任何设置上下文的内容。没有__enter__
or__exit__
功能,TestCase
也没有扩展任何东西。
解决方案
推荐阅读
- android - 我怎么知道特定协程调度程序使用了多少线程?
- xml - 即使有错误,大文件的 XML 验证也会通过
- angular - PrimeNG TurboTable - 奇怪的虚拟滚动行为 - 不断加倍 event.rows
- ios - L2CAP IOS + Linux (Bluez)
- node.js - 检查mongo中的子字符串
- php - PHP:警告:count():参数必须是数组或实现 Countable 的对象
- node.js - 如何从给定的起点获取已排序的记录
- java - java客户端调用python服务器报错org.apache.thrift.TApplicationException: Internal error
- docker - Dockerfile 找不到复制的文件夹
- batch-file - 批量使用 VBScript 中的变量