2017-02-10 45 views
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) 
+0

當你想要的東西發生時,你可以直接從函數中返回,然後放置一行會導致失敗的行(如果沒有命中返回)。 – khelwood

回答

1

測試總是通過,以防沒有指示失敗。萬一一切都好,就不要做任何事情。

+0

其實。我修改了我的代碼以滿足我的需求。 'try:function_that_raisesCustomWarning()\ nCustomWarning as w:pass'。 – notalentgeek