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

后端 未结 5 1850
悲&欢浪女
悲&欢浪女 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:15

    Building on Nate's answer I have this:

    def make_test_wrapper_for(command_module):
        def _run_cmd_with(*args):
            """Run the possibly_add_alert command with the supplied arguments"""
            cmd = command_module.Command()
            (opts, args) = OptionParser(option_list=cmd.option_list).parse_args(list(args))
            cmd.handle(*args, **vars(opts))
        return _run_cmd_with
    

    Usage:

    from myapp.management import mycommand
    cmd_runner = make_test_wrapper_for(mycommand)
    cmd_runner("foo", "bar")
    

    The advantage here being that if you've used additional options and OptParse, this will sort the out for you. It isn't quite perfect - and it doesn't pipe outputs yet - but it will use the test database. You can then test for database effects.

    I am sure use of Micheal Foords mock module and also rewiring stdout for the duration of a test would mean you could get some more out of this technique too - test the output, exit conditions etc.

提交回复
热议问题