Why not use the python assert statement in tests these days?

In Python tests, we should write:

self.assertEqual(response.status_code, 200) self.assertIn('key', my_dict) self.assertIsNotNone(thing) 

In contrast to the natural:

 assert response.status_code == 200 assert 'key' in my_dict assert thing is not None 

In many cases, error messages generated by helper assertion methods are no longer readable (perhaps the unelest style for camelCase is less readable).

The history of my project:

  • We always use modern test videos like nose or py.test, and we don’t really care about unittest runner. Opening a test doesn't even care if the class ultimately unittest.TestCase , or whether you even work inside the class.
  • We will never run automatic tests with the -O switch for the interpreter.
  • When we want to get a more readable message, by default one of unittest is not good enough at all - for example, there is one value that is different, embedded in json data somewhere, and we want to see contextual diff.

CPython core libraries and tests are still typically written in unittest style, and they usually do everything for a good reason. So my question is: is it ok to work outside the unittest style, or are there some shortcomings that I am missing - why not just use assert in my tests? Why not disconnect core developers from unittest yet?


: docs mention reason:

These methods are used instead of approval, so a test runner can accumulate all test results and create a report.

However, this excuse seems fictitious, the test runner can accumulate results and report as usual, even if you use assert statements. The unutbu post shows that unittest will raise the value of AssertionError in the same way as the assert statement, and that was over 7 years ago, so this is not a brilliant new feature.

+7
python unit-testing nose
source share
2 answers

The link to the documents found is the correct answer. If you don't like this style of writing tests, I would suggest using pytest:

http://pytest.org/latest/

pytest has done a ton of work that allows you to use the assert statement the way you want. It also has many other really nice features, such as their lights.

-one
source share

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 # unittest style self.assertEqual(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.

+3
source share

All Articles