Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not use python's assert statement in tests, these days?

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?

like image 685
wim Avatar asked Nov 03 '15 05:11

wim


People also ask

Is it good to use assert Python?

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.

Under what circumstances assert statements are not executed in Python?

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.

Should I use assert?

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.

Should I use assert in code?

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.


1 Answers

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.

like image 65
warownia1 Avatar answered Nov 15 '22 15:11

warownia1