Unit Testing in Python with Pytest (85/100 Days of Python)

Martin Mirakyan
4 min readMar 27, 2023

--

Day 85 of the “100 Days of Python” blog post series covering unit testing

Testing is an essential part of the software development process. It allows developers to ensure that their code is working as expected and to catch bugs before they are deployed to production. In Python, there are several testing frameworks available, including unittest, doctest, and pytest. In this tutorial, we will focus on using pytest, a popular testing framework that offers a lot of features and flexibility.

Why Is Testing Important?

  1. Ensure code quality: Testing helps ensure that the code is working as expected and meets the requirements. It also helps catch bugs and issues before they are deployed to production.
  2. Faster development: By catching bugs early, testing can speed up the development process, as developers can fix issues quickly and avoid lengthy debugging sessions later on.
  3. Better collaboration: Tests can help ensure that code changes do not break existing functionality, making it easier for developers to collaborate and work on the same codebase.
  4. Easier maintenance: Tests can serve as documentation and help developers understand how different parts of the code work.

How to Use Pytest for Unit Testing in Python

To get started with pytest, you first need to install it. You can do this using pip, which is the Python package manager. Open up a terminal or command prompt and type the following command:

pip install pytest

After that, to write tests in pytest, you need to create a test file that contains test functions. Test functions should be named starting with “test_” and should take no arguments. Here is an example of a test function:

def test_addition():
assert 2 + 2 == 4

In this example, the test function checks if 2 + 2 equals 4. The assert statement is used to verify that the expression on the right-hand side is true. If the expression is False, pytest will raise an AssertionError.

To run tests with pytest, you need to execute the pytest command followed by the name of the test file. For example, if your test file is named test_math.py, you can run the tests by executing the following command:

pytest test_math.py

By default, pytest will discover and run all test functions in the specified file.

Pytest can also generate a coverage report, which shows how much of the code is covered by the tests. To generate a coverage report, you need to install the coverage package and then run pytest with the — cov option followed by the name of the package or module you want to generate coverage for:

pip install coverage
pytest --cov=my_package

This will generate a coverage report that shows which lines of code are covered by the tests.

We will discuss test coverage in much more detail in the future posts.

Best Practices to Follow When Writing Tests

There are several best practices that can help ensure that your unit tests are effective and maintainable:

  1. Write tests early and often: Tests should be written as soon as possible in the development process, and new tests should be added as code changes. This helps catch bugs early and ensures that new code does not break existing functionality.
  2. Keep tests small and focused: Each test should focus on a single piece of functionality and be as small as possible. This makes it easier to isolate and fix issues when they occur.
  3. Use descriptive test names: Test names should describe the behavior being tested, making it easier to understand the purpose of the test.
  4. Avoid testing implementation details: Tests should focus on the public interface of a module or class and avoid testing implementation details. This makes it easier to refactor code without breaking tests.
  5. Use test fixtures: Fixtures can help set up the testing environment and provide a fixed baseline for tests to run against. This can make tests more reliable and easier to write.
  6. Use mocking and stubbing when necessary: Mocking and stubbing can help isolate tests and make them faster and more reliable. However, they should be used sparingly and only when necessary.
  7. Run tests automatically: Tests should be run automatically as part of the build process, and developers should be notified if any tests fail. This helps catch issues early and ensures that code changes do not break existing functionality.
  8. Maintain code coverage: Code coverage measures how much of the code is covered by tests. Maintaining a high code coverage ensures that tests are thorough and helps catch issues before they are deployed to production.

What’s next?

--

--