Posts tagged ‘python’


time-machine versus freezegun, a benchmark

It’s a bench, mark.

I wrote my library time-machine last year as a way to speed up tests that need to accurately mock the current time.

Read more...

Cheap Bug Protection With pre-commit’s Regex Hooks

You aren’t allowed to write that!

For all my linting needs these days I use the pre-commit framework. It has integrations with every tool I want to use, and uses Git’s hooks to prevent non-passing code from ever being committed.

Read more...

How to Limit Test Time in Django’s Test Framework

Take some time out

I recently optimized a client project’s test suite, and in the process found a test whose runtime had crept up ever since it had been written. The problematic test exercised an import process from a fixed past date until the current day. The test’s runtime therefore grew every day, until it reached over a minute.

Read more...

How to Override the gunicorn Server Header

A sheepicorn, and I swear it was green.

Update (2021-04-02): This is no longer needed since the release of gunicorn 20.1.0, which no longer sends the version in the `Server` header.

Read more...

How to Mock the Current Date and Time in Python

The power of time in the palm of my hand!

If you’re testing Python code that relies on the current date or time, you will probably want to mock time to test different scenarios. For example, what happens when you run a certain piece of code on February 29? (A common source of bugs.)

Read more...

Cyber Monday discount for Speed Up Your Django Tests

Back from the first Cyber Monday...

Earlier this week I tweeted about my two part offer on my book Speed Up Your Django Tests for this year’s Cyber Monday.

Read more...

How to Mock Environment Variables in Python’s unittest

Our environment - mother gaia

Sometimes tests need to change environment variables. This is fairly straightforward in tests using Python’s unittest, thanks to os.environ quacking like a dict, and the unittest.mock.patch.dict decorator/context manager.

Read more...

How to Mock Environment Variables in pytest

Our environment - planet earth

Sometimes tests need to change environment variables. This is fairly straightforward in pytest, thanks to os.environ quacking like a dict, and the unittest.mock.patch.dict decorator/context manager.

Read more...

How to Unit Test a Django Management Command

Here comes the test train!

This post is an adapted extract from my book Speed Up Your Django Tests, available now.

Read more...

A Guide to Python Lambda Functions

It’s a Lamb, Duh

This is a cross-post from the Scout APM blog, where I occasionally write. I also maintain the Scout Python integration.

Read more...

All tags: