The key difference between using the assert keyword or dedicated methods is the output report. Note that the statement following assert is always True or False and cannot contain any additional information.
assert 3 == 4
will just show AssertionError in the report. Nevertheless,
self.assertTrue(3 == 4)
Gives additional information: AssertionError: False is not true . Not very useful, but consider:
self.assertEqual(3, 4)
This is much better as it tells you that AssertionError: 3 != 4 . You read the report, and you know what this statement (equality test) and meaning was.
Suppose you have a function and want to return the value that it returns. You can do this in two ways:
# assert statement assert your_function_to_test() == expected_result
In the event of an error, the first one does not give you any information except the statement error, the second tells you what type of statement (equality test) and what values ββare involved (value returned and expected).
For small projects, I never worry about the unittest style, as it is longer for input, but in large projects you can learn more about the error.
warownia1
source share