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

not*_*eek 5 python warnings unit-testing 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)
Run Code Online (Sandbox Code Playgroud)

lan*_*tar 8

如果没有迹象表明测试失败,则测试始终会通过。只是不要做任何事情,以防万一一切都好。