From the course: Create an Open-Source Project in Python

What is pytest?

- [Instructor] Welcome to the chapter where we'll learn how to write better tests for our programs. The testing library we are going to use is called pytest. Pytest is a Python testing framework that allows you to write small tests such as unit tests with ease. The standard testing library that comes with a CPython distribution is called unittest. However, I prefer pytest for reasons we'll discuss later. Pytest is very popular. It has over 9,000 stars on the GitHub page, which for an open-source project is very good. And due to its popularity, several plug-ins for pytest have been developed, such as pytest-cov for test code coverage, pytest-django for testing Django, a Python web framework, and pytest-asyncio for testing asynchronous code. Now, as I mentioned earlier, a lot of Python developers prefer pytest over unittest. The first reason is pytest provides several powerful built-in functions that make managing your test very easy. Second, pytest structures tests in a way that makes it easier to work with. In unittest, a test case class has to be defined and the tests are written inside it. But with pytest, you simply write your test functions and assert if your code is correct inside a much more straightforward process. A third advantage pytest has is the many plug-ins available for it. As I mentioned before, there are plug-ins to help with testing several different applications. Finally, pytest provides a better-looking error report. It shows how many tests have failed, how many tests were skipped, and how many tests have passed in a nicely laid out summary from which you can dive into the fail tests and resolve them. Pytest is a very useful tool for testing. Let's see how we can use it to test our project.

Contents