管理命令可以用 call_command()
函数来测试。输出可以重定向到 StringIO
实例中:
from io import StringIO
from django.core.management import call_command
from django.test import TestCase
class ClosepollTest(TestCase):
def test_command_output(self):
out = StringIO()
call_command('closepoll', stdout=out)
self.assertIn('Expected output', out.getvalue())
以上内容是否对您有帮助:
更多建议: