Foreword
pytest is one of the projects that show off the best qualities of Python; it is the easiest Python testing framework to get started and also the most powerful. The pytest developers leverage metaprogramming to help users; we can write tests as plain functions and use the
assert keyword for checks, which means we can use any Python expression as a check, and we don't need to memorize the names of dozens of assertThis, assertThat methods. When there is a test failure, pytest uses sophisticated introspection to show the difference between the expected value and the value returned by the code under test. Introspection and decorators are also used to provide fixtures, parameters, grouping, skipping, and such like.
pytest is basically two parts: a library that gives you the API to write tests, and a test runner, a command-line utility that can search and execute tests across your project packages. The behavior of the pytest CLI can be configured in many ways, as described in this guide. By default, pytest is smart enough to collect tests written with the standard unittest package as well as doctest, running them and reporting all their results together. This means you can start using pytest now to run the tests you've already written with unittest and doctest.
pytest is very powerful out of the box, but it can also be enhanced by a large collection of industrial-strength plugins. A good collection of the best plugins is covered in this book.
If you are in the business of writing libraries and APIs for others to use, pytest is not only a great tool, but also a plentiful source of ideas and techniques, leveraging the best features of Python.
Anyone willing to learn pytest will be lucky to be guided by Bruno Oliveira, a long-time core developer of this most practical, powerful, and Pythonic package.
Thank you for your work on pytest, Bruno and for writing this book.
Valeu!
Luciano Ramalho
Principal consultant at Thoughtworks and author of Fluent Python