0

I want to check multiple assertion like this, and after first line code is throwing an assertion error. can someone help me how to catch this assertion. I know it can be possible through try-except, but is there any other way of doing it in unit test style. Please also tell me, what is the reason this functionality is not same like multiple AssertTrue, AssertFalse methods?

import unittest

class Test1(unittest.TestCase):
    def test_between(self):
        print ("before")
        self.assertLessEqual(999, 998, "Not less")  # After this next line is not executing because this is throwing an "AssertionError: 999 not less than or equal to 998 : Not less"
                                                    # How can I catch this error, I know one way is through try,Except... but is there any proper solution available?
        print ("after")
        self.assertLessEqual(999, 500, "Not less")  # 2nd Assertion

if __name__ == '__main__':
    unittest.main()

Any kind of help is appreciated.

Regards

1 Answer 1

1

One method you could try is assertRaises and call the assertLessEqual method from within the with block:

import unittest


class Test1(unittest.TestCase):
    def test_between(self):
        with self.assertRaises(AssertionError) as e:
            self.assertLessEqual(999, 998, "Not less")

        with self.assertRaises(AssertionError) as e:
            self.assertLessEqual(999, 500, "Not less")


if __name__ == '__main__':
    unittest.main()

You should find that the behaviour is the same with assertTrue or assertFalse as with assertLessEqual which has the following description:

Just like self.assertTrue(a <= b), but with a nicer default message.

You should find that many of the unit test methods are simply nice wrappers for an assertTrue or assertFalse call.

Of course, a simplistic approach to solving your problem would be to either rearrange a and b in your assertLessEqual call or user assertGreaterEqual instead.


If you have a large number of similar tests to run, you could also look at subTest. This allows you to run the same test code for a number of variables:

import unittest

class Test1(unittest.TestCase):
    def test_between(self):
        for i in range(10):
            with self.subTest(i=i):
                self.assertLessEqual(i, 5)

if __name__ == '__main__':
    unittest.main()

The above test results in 4 failures, an example of one:

FAIL: test_between (__main__.Test1) (i=9)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "scratchpad.py", line 7, in test_between
    self.assertLessEqual(i, 5)
AssertionError: 9 not less than or equal to 5
Sign up to request clarification or add additional context in comments.

4 Comments

self.assertLessEqual(999, 1000, "Not less") for this it is printing a message "AssertionError: AssertionError not raised" with lots of other messages also. I think it should only print if value is not less then as required and for other it should not print logs.
I have to loop over 100+ attributes. and if it is printing log messages for every one.. it will be difficult to dig out what is required. So it should print only for those which are failing test case.
Could you please clarify in your question exactly what you're trying to do. As it stands, this answers the original question you asked.
Oh great man. "subTest" this is what I wanted. Thank you for your valuable time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.