In Python testing, why would you use assert methods:
self.assertEqual(response.status_code, 200)
self.assertIn('key', my_dict)
self.assertIsNotNone(thing)
As opposed to the direct assertions:
assert response.status_code == 200
assert 'key' in my_dict
assert thing is not None
According to the docs:
These methods are used instead of the assert statement so the test runner can accumulate all test results and produce a report
However this seems to be bogus, a test runner can accumulate results and produce a report regardless. In a related post unutbu has shown that unittest will raise an AssertionError
just the same as the assert statement will, and that was over 7 years ago so it's not a shiny new feature either.
With a modern test runner such as pytest, the failure messages generated by the assertion helper methods aren't any more readable (arguably the camelCase style of unittest is less readable). So, why not just use assert statements in your tests? What are the perceived disadvantages and why haven't important projects such as CPython moved away from unittest yet?
Python's assert statement is a debugging aid, not a mechanism for handling run-time errors. The goal of using assertions is to let developers find the likely root cause of a bug more quickly. An assertion error should never be raised unless there's a bug in your program.
What Are Assertions & What Are They Good For? Python's assert statement is a debugging aid that tests a condition. If the condition is true, it does nothing and your program just continues to execute. But if the assert condition evaluates to false, it raises an AssertionError exception with an optional error message.
You can use an assert to check if your logical assumption is correct. You can also use assert statements to check if the control flow is correct or not. For example, if you have a function that returns a value, you may want to put an assert statement. However, you may get a 'non-reachable' code error.
The assert statement is an effective way to document code. For example, if you want to state that a specific condition should always be true in your code, then assert condition can be better and more effective than a comment or a docstring, as you'll learn in a moment.
The key difference between using assert
keyword or dedicated methods is the output report. Note that the statement following assert
is always True
or False
and can't contain any extra information.
assert 3 == 4
will simply show an AssertionError
in the report.
However,
self.assertTrue(3 == 4)
Gives some extra info: AssertionError: False is not true
. Not very helpful, but consider:
self.assertEqual(3, 4)
It's much better as it tells you that AssertionError: 3 != 4
. You read the report and you know what kind of assertion it was (equality test) and values involved.
Suppose you have some function, and want to assert value it returns. You can do it in two ways:
# assert statement
assert your_function_to_test() == expected_result
# unittest style
self.assertEqual(your_function_to_test(), expected_result)
In case of error, the first one gives you no information besides the assertion error, the second one tells you what is the type of assertion (equality test) and what values are involved (value returned and expected).
For small projects I never bother with unittest style as it's longer to type, but in big projects you may want to know more about the error.
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