Use autospeccing for your mocks in Python

Mocks are a powerful concept in testing. They are one of several types of test doubles, which are objects that can be used in place of real objects in your tests. Mocks are used to isolate the code under test from the rest of the system, and to verify that the code under test interacts with its dependencies correctly. However, if not used properly, mocks can lead to false positives in your tests. One common pitfall is that mocks can be too permissive, allowing you to call methods that don’t exist on the real object. This can lead to tests that pass even when the code under test is broken. ...

February 5, 2024

Supercharge Your Python TDD Workflow With pytest-watcher

If you follow the Test-driven Development practice in your Python projects, you need to run your test suite often. Having to run it manually can become tedious. You can configure handy shortcuts in your favorite IDE to make the process easier. But there is even better way using pytest-watcher. What is pytest-watcher? pytest-watcher is a continuous test runner for Python projects that reruns your tests whenever you change a *.py file inside your project. ...

June 8, 2023

Unlocking the power of asyncio Semaphore

When building asynchronous applications, oftentimes you need to limit the number of simultaneous connections to a shared resource. It can be your internal server, or an API that has usage limits. asyncio library provides a dedicated synchronization primitive Semaphore created exactly for this purpose. However, let’s first try to solve this problem without using it, in order to fully appreciate the value of this mechanism. We can limit the number of simultaneous connections by using a counter variable that will be incremented whenever we start making a request and decremented when we receive our response. Let’s look at the example code: ...

May 31, 2023

Optimizing your Django tests

If you are working on a large Django project, you probably have lots of automated tests running as part of your CI/CD process. As long as tests run fast, everyone is happy. But as your application grows in complexity, your tests start to take more and more time to run and eventually become a real bottleneck. In this post, I will share some ideas that can help you optimize runtime of your test suite. I assume you are using pytest, but recommendations described in this post should be easily applicable to other runners as well. ...

September 16, 2021