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)
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.
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.
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.
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.)
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
orstr
or any type that a subclass registers withaddTypeEqualityFunc()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With