0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Django】manage.pyで動くプロセス内からさらに自作したコマンドを叩く

Posted at

記録用に。

やりたかったこと

commandsディレクトリに自作のコマンドを作成し、テスト内から下記コマンドと同様のことを行おうとしました。

$ python manage.py create_initial_data

テストのセットアップ時にシェルでコマンドを流すのかと思いましたが、Djangoはここはしっかりサポートされていました

やり方

対象のスクリプトに、 call_command 関数を埋め込みます。

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


class TestSendEmailView(TestCase):
    def setUp(self):
        self.client = Client()
        call_command('create_initial_data') # ここ

    def test_success(self):
        response = self.client.get('/emails/')
        self.assertEqual(response.status_code, 200)

これで python manage.py create_initial_data と同じことが行われます。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?