While unit tests have become a standard for ensuring that the code performs as expected, they focus on checking the logic, not the performance. Yet, p

One pytest Marker to Track the Performance of Your Tests

submited by
Style Pass
2024-05-02 12:30:06

While unit tests have become a standard for ensuring that the code performs as expected, they focus on checking the logic, not the performance. Yet, performance regressions can be just as dangerous as functional bugs, putting your whole software at risk. This is why performance should be checked early while testing the code, the icing on the cake being to check it in CI environments.

Benchmarking is a way of testing a block of code's performance. It is like a test case but for performance. It will execute the code and measure how long it takes to run.

Let's see how to implement that with pytest. First, install the pytest-codspeed library to enable the benchmark marker and fixture:

Then, you're ready to use the @pytest.mark.benchmark marker for measuring performance. You can use it directly on a single test:

But you can also apply it at the module level by using the global pytestmark variable, effectively enabling performance testing on all the tests contained within it:

Leave a Comment