3 ms·
It has an excellent runner and makes much better use of Python features: * you can define tests as free functions (you don't have to, it also supports classes
by masklinn 9y ago
It has an excellent runner and makes much better use of Python features:
* you can define tests as free functions (you don't have to, it also supports classes and even regular unittest cases, you can use pytest to run your existing suite)
* exceptions aside it uses the standard `assert` statement
* fixtures are DI'd, independent from tests and much more flexible than the setUp/tearDown dance
* tests are configured and fixtures are defined using decorators & generators
* excellent support for data-driven tests (parametrized tests and fixtures)
* there is a wealth of reusable third-party plugins[0] ranging from framework helpers[1] to test parallelisation and distribution[2]
[0] http://plugincompat.herokuapp.com http://plugincompat.herokuapp.com
[1] https://pypi.python.org/pypi/pytest-tornado https://pypi.python.org/pypi/pytest-tornado
[2] https://pypi.python.org/pypi/pytest-xdist https://pypi.python.org/pypi/pytest-xdist
- kstrauser 9y ago> fixtures are DI'd, independent from tests and much more flexible than the setUp/tearDown dance If this were the only difference, it'd still be enough. Inevitably I end up with a unittest.TestCase with 30 tests, all of which mock out some resource. Except for 1. That one wants to mock out a different resource while leaving the first intact. * With unittest: either split that test out into its own TestCase (duplicating most of the original, except for the part I don't want, or coming up with an inheritance tree with is always ugly with unittest), or * Remove all the unwanted mocks inside that test. * With pytest: don't pass the mocking fixture into that test.
- masklinn 9y agoYup. The ability to compose fixtures too, a test can depend on a fixture but so can an other fixture. Combined with flexible scopes and the ability to mark and select/deselect tests based on name and marks it makes for genuinely great stuff e.g. some of your tests depend on the database, you can have a session-scoped fixture handling a connection, then a function-scoped one depending on it and getting a cursor, and if you don't select any test depending on the latter no connection will be created at any point.