Python Testing Tips
Take your skills to the next level
Time Travel in Your Tests with freezegun
Testing time-dependent code is notoriously tricky. If your code checks whether a subscription is active, validates trial eligibility, or performs any date calculations, your tests can become flaky and unreliable. Enter freezegun - a library that lets you freeze
How to mock different return values for each call of a method
When the same method is called multiple times in your code, you often need each call to behave differently — for example, fail first and then succeed on retry. Instead of writing multiple mocks or custom logic, you can
How to customize assertion error message
Many times, it's hard to understand why a test is failing just by reading the assertion error. For example, when writing end-to-end tests and asserting the expected status code. Without a custom message, you'll know only that the API
Name parameterized pytest examples
Text parametrization is a great feature provided by pytest. We've touched it inside the Parametrize tests using pytest article. Anyhow, it's also easy to make things unreadable when there are lots of examples or lots of parametrized values. We want
create_autospec – easily ensure that mocked objects are called correctly
When writing automated tests, we aim towards fast & reliable tests. Many times we need to use test doubles to ensure that. In Python, we quite often use MagicMock to replace objects during tests. It comes very handy, but its
Parametrize tests using pytest
The goal of tests is to make sure that our software is working as expected. Sometimes, it's enough to use a single example to prove that things meet our expectations. For example, when testing, we can fetch a user
Testing behavior, not implementation details – part 3
One of the properties of a valuable test is that it's fast. While tests with a database might be fast enough for simple projects, their execution times quickly add up. As mentioned in "Running tests in parallel with pytest", you
Testing behavior, not implementation details – part 2
In "Testing Behavior, not implementation details - part 1", we've briefly touched on the term observable behavior. But what is observable behavior anyway? Explaining this as "whatever you see software doing" doesn't help much. Especially not when we are talking
Running tests in parallel with pytest
Running tests takes time. Even if they are insanely fast. The execution time adds up once you add more and more tests to your project. You might not notice that when you have 100 test. You'll for sure notice
Testing behavior, not implementation details – part 1
When you want to learn about software testing, you quickly hear the famous "Test behavior, not implementation details". More often than not, it doesn't help you much. I know that - it was the same for me. So what does
Factory Fixtures
Factory fixtures, what in the whole earth would that be? When you want to test your code thoroughly, you have to write quite a few tests. This way, you ensure that your code is working as expected. Nevertheless, you
Single assertion per test – rule or guideline?
Python Testing Tip #1 Why do we even test our software? If you've read anything about software testing, I'm sure you've stumbled across the idea of "one assertion per test". Sounds like a simple idea, right? Thinking about
Subscribe To Python Testing Tips
Get Python Testing Tips to Your Inbox
Subscribe To Python Testing Tips
Get Python Testing Tips to Your Inbox