Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using assertTrue(==) vs assertEqual in unittest

In the Python unittest module, are there any advantages or disadvantages of using assertTrue() vs. assertEqual() in the following case?

self.assertTrue(a == b)
self.assertEqual(a, b)
like image 759
jersey bean Avatar asked Jan 11 '18 21:01

jersey bean


People also ask

What is the purpose of the assertEqual method in the unittest?

assertEqual() in Python is a unittest library function that is used in unit testing to check the equality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are equal assertEqual() will return true else return false.

What is difference between assertTrue and AssertEquals?

AssertEquals method compares the expected result with that of the actual result. It throws an AssertionError if the expected result does not match with that of the actual result and terminates the program execution at the assertequals method. AssertTrue method asserts that a specified condition is true.

What does assertTrue do in Python?

assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return false.

Is PyUnit and unittest same?

PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)


1 Answers

Always use assertEqual(), as it customizes failure output.

The method delegates to various helper methods to show you how, for example, two strings or two lists differ when the assertion fails, provided the type of both arguments match and have a type-specific helper method registered.

assertTrue() can only tell you about the assertion failing, not show you why.

From the assertEqual() documentation:

In addition, if first and second are the exact same type and one of list, tuple, dict, set, frozenset or str or any type that a subclass registers with addTypeEqualityFunc() the type-specific equality function will be called in order to generate a more useful default error message (see also the list of type-specific methods).

Only use assertTrue() if there is no more specific assertion available.

like image 81
Martijn Pieters Avatar answered Sep 21 '22 21:09

Martijn Pieters