0

I am trying to print the item that rose as exception when I run my unit tests. For example:

def test_string(self):
   my_str = "new string"
   self.assertTrue(len(my_str) == 5)

This is a very simplified example but it shows what I am trying to achieve. I want, in that case, for the test to print "new string" after I get 'F'.

In regular assert I can achieve that by doing:

assert (len(my_str) == 5), "new string" 

But when using unittest it won't work.

Is there a way to do that with unittest ?

2
  • this is an example ofc Commented Jul 5, 2021 at 10:04
  • For starters, use self.assertEqual(len(my_str), 5). Also, use pytest if you can, then you can write regular asserts and it'll still show you the failing values. Commented Jul 5, 2021 at 10:45

1 Answer 1

2

You can use

 self.assertTrue(len(str) == 5, msg="new string")

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue

Sign up to request clarification or add additional context in comments.

Comments

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.