lundi 2 novembre 2015

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

When testing in python, is there any disadvantage to using this style:

assert response.status_code == 200

assert 'key' in my_dict

assert thing is not None

As opposed to the unittest style:

self.assertEqual(response.status_code, 200)

self.assertIn('key', my_dict)

self.assertIsNotNone(thing)

I understand that the general idea of all those helpers such as self.assertTrue, self.assertIn etc is to generate more readable failure messages. But for many simple cases, those messages aren't really any more readable, honestly, and the unpythonic-looking code when trying to write in the camelCase style of unittest is arguably less readable.

Background of my project:

  • We're always using modern test runners such as nose or py.test, and we don't really care about unittest runner. Test discovery doesn't even care whether or not the class ultimately inherits unittest.TestCase, or whether you work inside a class at all for that matter.
  • We will never execute the automated tests with the -O switch to interpreter.
  • When we want a more readable message, the default one from unittest is generally not good enough anyway - e.g. one value differing deeply nested in a json data structure, we want to see a contextual diff. So we'll write a helper method/context manager for those complex cases.

Core libraries and cpython tests are still generally written in unittest style, and they generally do everything for a good reason. So my question is, do you think it's OK to work outside the style of unittest, or are there some disadvantages I don't know about?


addendum: the docs do mention a reason:

These methods are used instead of the assert statement so the test runner can accumulate all test results and produce a report

However this justification seems to be bollocks, the test runner can accumulate results and report as usual even if you use assertion statements. In a related post unutbu has shown that unittest will raise an AssertionError just the same as the assert statement will, and that was more than 5 years ago so it's not a shiny new feature.

Aucun commentaire:

Enregistrer un commentaire