1
我正在使用這個self.assertTrue(True)
來通過測試。我想要的是通過測試,如果一個特定的警告提出。是否有`unittest.TestCase.fail()`通過Python單元測試的相反方法?
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)
當你想要的東西發生時,你可以直接從函數中返回,然後放置一行會導致失敗的行(如果沒有命中返回)。 – khelwood