notalentgeek
notalentgeek

Reputation: 5839

Is there an opposite methods to `unittest.TestCase.fail()` to pass a Python unit test?

I am currently using this self.assertTrue(True) to pass the test. What I want is to pass a test if a specific warning is raised.

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)

Upvotes: 5

Views: 1210

Answers (1)

languitar
languitar

Reputation: 6794

A test always passes, in case there is no indication that it failed. Just don't do anything in case everything is alright.

Upvotes: 8

Related Questions