การสร้างคำตอบของเนทฉันมีสิ่งนี้:
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
การใช้งาน:
from myapp.management import mycommand
cmd_runner = make_test_wrapper_for(mycommand)
cmd_runner("foo", "bar")
ข้อได้เปรียบตรงนี้คือถ้าคุณใช้ตัวเลือกเพิ่มเติมและ OptParse สิ่งนี้จะเรียงลำดับให้คุณ มันยังไม่สมบูรณ์แบบ - และมันยังไม่ได้ไพพ์เอาท์พุท - แต่มันจะใช้ฐานข้อมูลทดสอบ จากนั้นคุณสามารถทดสอบเอฟเฟกต์ฐานข้อมูล
ฉันแน่ใจว่าการใช้โมดูลจำลองของ Micheal Foords และการเดินสาย stdout อีกครั้งในระหว่างการทดสอบจะหมายความว่าคุณสามารถใช้เทคนิคนี้ได้มากขึ้นเช่นกัน - ทดสอบเอาต์พุตเงื่อนไขการออก ฯลฯ