Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sosmii/b8ea5f0e66d70681cdaad6f56a9c62dd to your computer and use it in GitHub Desktop.
Save sosmii/b8ea5f0e66d70681cdaad6f56a9c62dd to your computer and use it in GitHub Desktop.

Django Commandsの標準出力をテストする一番簡単な方法

ググっても簡単な方法がパッと出てこなかったので書く。
StringIOを使うのが多分一番楽だと思う。

from io import StringIO

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


class CommandTest(TestCase):
    def test_a(self):
        out = StringIO()
        err = StringIO()
        call_command('some_command', stdout=out, stderr=err)

        self.assertIn('some text', out.getvalue())
        self.assertIn('some text', err.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment