During many years of working as Python developer and tester, I have seen many ways to deal with testing. Having many choices such as ‘nose’ and ‘unittest’ may confuse many of developers what to choose. For me, the decision was always easy: Pytest!
Pytest makes test suites more readable, more flexible and compact to the language itself. Pytest helps us write tests faster as well as keep them maintainable. In addition to these, we can use many fixtures and plugins which Pytest community provided.
Pytest Pros and Advantages
1. Easy to read
Yes, just like this!
2. Easy to write !
3. Tests discovery
Pytest can automatically finds tests we’ve written, runs the tests, and reports the results. It has a library of goodies that we can use in our tests to help us test more effectively.
4. CI/CD Integration
Pytest integrates easily with other tools like continuous integration and web automation.
5. Compact test suites
Pytest introduced the concept that Python tests should be plain Python functions instead of forcing developers to include their tests inside large test classes.
6. Detailed and useful failure information
Pytest rewrites tests in a way that it can store intermediate values. As a result in case of failing it provides detailed explanation about what has been asserted and what have failed.
7. Fixtures are simple and easy to use
A fixture is a function that returns a value and to use a fixture you just have to add an argument to your test function. It is also possible to parametrize fixtures and every test that uses it will run with all values of parameters. This will eliminate code rewrite.
8. Many plugins are available to serve us!
Pytest can easily be extended with several hooks, and the same team develops a number of very useful plugins.
9. Tests parametrize
We can parametrize any test and cover all uses of a unit without any code duplication.
10. Test discovery by file-path
We just need to indicate the path which includes your tests, and Pytest will collect them
We can choose what to run, what to skip and what to execute in case of some conditions
12. Community support
It is being actively developed and maintained by a passionate and growing community.
13. Works for old-fashions too!
Pytest is installed separately from your Python version, so you can use the same latest version of pytest on legacy Python 2 (2.6+) and Python 3 (3.3+).