python - 是否有与 `unittest.TestCase.fail()` 相反的方法来通过 Python 单元测试?

标签 python unit-testing warnings python-unittest

我目前正在使用这个self.assertTrue(True)来通过测试。我想要的是在出现特定警告时通过测试。

import warnings
class test(unittest.TestCase):
    def test_1(self):
        with warning.catch_warnings(record=False):
            warning.simplefilter("error", category=CustomWarning)
            try: function_that_raisesCustomWarning()
            except CustomWarning as w: self.assertTrue(True)

最佳答案

如果没有迹象表明测试失败,测试总是会通过。只是不要做任何事情,以防万一一切正常。

关于python - 是否有与 `unittest.TestCase.fail()` 相反的方法来通过 Python 单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42161607/

相关文章:

python - pybind11 将 python sys.stdout 从 print() 重定向到 C++

python - 在 Apache Nifi ExecuteStreamCommand 中运行 python 代码

visual-studio - 运行所有测试时出现MS Test错误

c# - 如何配置 TeamCity 以同时运行测试而不是一个接一个地运行?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

xcode - 如何更改 Xcode 4 中文本突出显示的颜色以显示警告和错误?

python - threading.local 是否使用锁进行访问?

python - pypyodbc:关键字 "WITH"附近的 OPENJSON 语法不正确

android - 如何测试自定义 cordova 插件 native 代码?

makefile - 如何将警告视为 Makefile 中的错误?