python - When is unittest.addCleanup() executed?
问题描述
I have some doubt with respect to addCleanup() --
- When addCleanup() will be executed, only when there is a failure in any steps or it will be executed in normal scenarios too (when there is no failure)?
- If there is some failure in test, tearDown() will be called but if some steps in tearDown() fails, can we add addCleanup() in tearDown() to call some function which can do a proper cleanup?
解决方案
添加一个要在 tearDown() 之后调用的函数以清理测试期间使用的资源。函数的调用顺序与添加顺序相反(LIFO)。
它们被添加时使用传递给 addCleanup() 的任何参数和关键字参数调用。如果 setUp() 失败,意味着 tearDown() 没有被调用,那么任何添加的清理函数仍然会被调用。
addCleanup (function, *args, **kwargs)
推荐阅读
- spring - 在 Spring-boot 中将 redis-cache 反序列化为对象的问题
- html - 具有自动多列文本换行的水平可滚动 div
- c++ - 为什么创建堆数组的时间复杂度不是 O(log(n!)) 而不是 O(nlogn)?
- vim - 如何粘贴到行首?
- c# - 使用事件按钮单击表单从用户控件更新 datagridview
- python - Python多进程如何在阻塞代码之前返回值
- java - 在应用程序文件中使用 .getWindow()
- android - 如何将图片扩展到布局的边缘
- haskell - TimeOfDay 的任意实例
- javascript - Firebase signInWithPopup() 函数,弹出 google 登录窗口但弹出窗口不显示任何 google 帐户