Member-only story

Testing Django Admin Commands

Make sure your commands don’t break

Bennett Garner
2 min readSep 27, 2021

Commands in Django allow you to write scripts for your application that you can run from the command line, using manage.py. Recently, I needed to write unit tests for custom Django commands.

This is the quick guide I wish I’d found first thing…

Why test custom commands?

Custom Django admin commands are meant to be useful helper scripts that automate certain processes. They can start tasks, create users, check for issues, or anything else you might want them to do.

If you don’t use commands heavily or they’re just simple helpers for use in development, then you might not need to write unit tests for them.

However, my team uses custom commands to automate complex processes, start background worker tasks, and create sandbox users on our application. We use custom commands heavily. Additionally, because they rely on other code in our application, they’re prone to breaking whenever the underlying structure is refactored.

For those reasons, we need unit tests for custom commands.

Unit Tests for Django Admin Commands

It turns out that writing tests for a Django admin command is fairly easy (even if…

--

--

Bennett Garner
Bennett Garner

Written by Bennett Garner

DeveloperPurpose.com — Build a coding career with meaning and purpose 💻 Top writer in technology ✍️

No responses yet