Testing Django Admin Commands

September 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 the steps are slightly buried in the documentation).

Testing tools | Django documentation | Django docs.djangoproject.com

Django provides a call_command() function in django.core.management that allows you to programmatically trigger a command from within your Python code.

So, in order to test a command:

Things to Note

  1. We captured stdout with a StringIO instance. That means you can do something like, self.assertIn('Expected output', out.getvalue())
  2. We can provide args and kwargs to the command using a list and a dictionary respectively
  3. Once you’ve called the command, it’s up to you to add assertions below about the state of your application after the command has run

Hope that helps!

Have questions about how to test Django admin commands? Send me an email or leave a comment below!

Like what you’ve read here?

I share my best content with my email list for free.

Join 500 other developers currently in my email series.


Profile picture

I write something new every day for 2k software developers. You should sign up for the daily email.

© 2024