カスタム django-admin コマンドをテストする方法 質問する

カスタム django-admin コマンドをテストする方法 質問する

わたしは作ったカスタム django-admin コマンド

しかし、どうやってテストすればいいのか分からない標準の Django テスト

ベストアンサー1

カバレッジ ツールを使用している場合は、次のようにコードから呼び出すとよいでしょう。

from django.core.management import call_command
from django.test import TestCase

class CommandsTestCase(TestCase):
    def test_mycommand(self):
        " Test my custom command."

        args = []
        opts = {}
        call_command('mycommand', *args, **opts)

        # Some Asserts.

公式ドキュメントより

管理コマンドはcall_command()関数でテストできます。出力はStringIOインスタンスにリダイレクトできます。

おすすめ記事