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

后端 未结 5 1853
悲&欢浪女
悲&欢浪女 2020-11-29 17:30

I want to write a unit test for a Django manage.py command that does a backend operation on a database table. How would I invoke the management command directly from code?

5条回答
  •  醉酒成梦
    2020-11-29 18:30

    Rather than do the call_command trick, you can run your task by doing:

    from myapp.management.commands import my_management_task
    cmd = my_management_task.Command()
    opts = {} # kwargs for your command -- lets you override stuff for testing...
    cmd.handle_noargs(**opts)
    

提交回复
热议问题