How can I call a custom Django manage.py command directly from a test driver?

The best way to test such things – extract needed functionality from command itself to standalone function or class. It helps to abstract from “command execution stuff” and write test without additional requirements.

But if you by some reason cannot decouple logic form command you can call it from any code using call_command method like this:

from django.core.management import call_command

call_command('my_command', 'foo', bar="baz")

Leave a Comment