157 Posts Tagged ‘python’ (Page 5)

(All tags.)


Django: convert a TestCase from using setUp() to setUpTestData()

Django’s TestCase class provides the setUpTestData() hook for creating your test data. It is faster than using the unittest setUp() hook because it creates the test data only once per test case, rather than per test.

Read more...

time-machine versus freezegun, a benchmark

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

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

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

In all current releases of the popular WSGI server gunicorn, the Server header reports the complete version of gunicorn. I spotted this on my new project DB Buddy. For example, with httpie to check the response headers:

Read more...

Python: mock the current date and time

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

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 pytest

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

Read more...

Python: mock environment variables with unittest

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

Read more...

Django: unit test a management command

When we write custom management commands, it’s easy to write integration tests for them with call_command(). This allows us to invoke the management command as it runs under manage.py, and retrieve the return code, standard output, and standard error. It’s great, but has some overhead, making our tests slower than necessary. If we have logic separated out of the command’s handle() method, it improves both readability and testability, as we can unit test it separately.

Read more...

A Guide to Python Lambda Functions

In Python, Lambda functions are rare compared to “normal” functions, and occasionally misunderstood or overused.

Read more...

Better Python Decorators with wrapt

A Python decorator wraps a target function with another wrapper function. This wrapper function can add any behavior you might want. For example, it can track execution times, redefine how the wrapped function runs, or modify return values.

Read more...

Why does Python raise ModuleNotFoundError when modifying Django’s INSTALLED_APPS?

Imagine we are installing the third party package django-cors-headers, which I maintain. Step one in its installation process is to install the package, so we run the command:

Read more...

How to Check if Python’s Output Buffering Is Enabled

By default, Python buffers output to standard output (stdout) and standard error (stderr). This means that output from your code might not show up immediately, making debugging harder.

Read more...

Python: fix SyntaxWarning: list indices must be integers or slices

Take this code, which we want to return a list of two breakfast orders:

Read more...

Python: fix SyntaxWarning: assertion is always true

Take this code:

Read more...

Python: fix SyntaxWarning: '<type>' object is not subscriptable

Take this code:

Read more...

Python: fix SyntaxWarning: '<type>' object is not callable

Take this code, which we want to return a list of shopping items with desired quantities:

Read more...

How to Unit Test a Django Form

Django’s test client is really useful for writing integration tests for your project. It’s great because it has a simple API for testing your application similarly to how a web browser would interact with it. Unfortunately it can be slow, because each call creates a request, passes it through all your middleware, view, maybe a template, then the response comes back through the same layers.

Read more...

Avoid Hardcoding IDs in Your Tests

This is a test anti-pattern I’ve seen creep in on many Django test suites. I know of several large projects where it became a major undertaking to undo it. The good news is it’s easy to avoid adding it when you first write your tests. 🙂

Read more...

Introducing time-machine, a New Python Library for Mocking the Current Time

Whilst writing Speed Up Your Django Tests, I wanted to add a section about mocking the current time. I knew of two libraries for such mocking, but I found it hard to pick one to recommend due to the trade-offs in each. So I delayed adding that section and shaved a rather large yak by writing a third library.

Read more...

The Fast Way to Test Django transaction.on_commit() Callbacks

Django’s transaction.on_commit() hook is useful for running tasks that rely on changes in the current database transaction. The database connection enqueues callback functions passed to on_commit, and executes the callbacks after the current transaction commits. If the transaction is rolled back, the callbacks are discarded. This means they act if-and-when the final version of the data is visible to other database connections.

Read more...

“Speed Up Your Django Tests” is Out Now

My previously announced book “Speed Up Your Django Tests” is out now on Gumroad. I’ve been writing since the 3rd March, so it’s quite a relief to have launched it.

Read more...

How to Check the Running Django Command

It’s occasionally useful to be able to tell which Django manage.py command is being run, in a code path that otherwise has no way of telling. For example, in Speed Up Your Django Tests, I describe how to modify manage.py to default use a test settings file when the test command is run.

Read more...

Book Announcement: Speed Up Your Django Tests

At the start of March I started writing a blog post called “How to Speed Up Your Django Tests”. Before I knew it, the outline alone was 4,000 words! I realized writing it up would be a major undertaking. As lockdown arrived, I found the time to write it all up.

Read more...