- Unittest has better error messages, this is no joke.
Although pytest is very verbose in describing errors, unittest provides a more informative "default" error message.
If the assertion fails, self.assertTrue(obj)
produces the message "obj is not true",
which is more informative than PyTest AssertionError.
-
unittest is more explicit:
self.assertTrue(obj)
is more explicit and clear thanassert obj
This improves the readability, maintainability and clarity of the code. Unittest requires from developer a more explicit declaration of what exactly is supposed to be tested, this increases also the testing understanding of the developer. -
autocomplete: If I want to select the best assertion for the current test, I can use autocomplete. I can see the list of assertions and select the best one for the current test.
-
consistency: If the assertion is aligned with the idea of the test - I can visualize what the developer wants to do in the test. Many developers fight over the right variable naming, and the right assertion is exactly the same. Many developers fight over type-hinting, and the right assertion is exactly the same, too.
-
Speed By default, without additional plugins, Pytest is 4 times slower than unittest.
In this repository you can check it out.
python -m timeit "import subprocess; subprocess.run(['pytest', 'tests'])" 1 loop, best of 5: 493 msec per loop
python -m timeit "import subprocess; subprocess.run(['python', '-m', 'unittest', 'discover', '-s=utests'])" 2 loops, best of 5: 119 msec per loop