[PYTHON] [Django] Hit a command you made from within the process that runs on manage.py.

For recording.

What I wanted to do

I created my own command in the commands directory and tried to do the same as the following command from within the test.

$ python manage.py create_initial_data

I was wondering if I would run commands in the shell when setting up the test, but Django was well supported here.

manner

Embed the call_command function in the target script.

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') #here

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

This will do the same thing as python manage.py create_initial_data.

Recommended Posts

[Django] Hit a command you made from within the process that runs on manage.py.
When you want to hit a UNIX command on Python
I made you to execute a command from a web browser
I made a neural network generator that runs on FPGA
Introducing youtube-dl, a video download tool that runs on the command line, and its zsh completion function.
I made a simple timer that can be started from the terminal
I made a webAPI! Build environment from Django Rest Framework 1 on EC2
You search commandlinefu on the command line
[Django] Create a form that automatically fills in the address from the zip code
CodePicnic that can create a [Development | Run | Tutorial | Demo] environment that runs on the browser
The Linux emulator "iSH" that runs on the iPad is a hot topic in me
I made a command to wait for Django to start until the DB is ready
I made a VM that runs OpenCV for Python
A programming language that protects the people from NHK
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (creating a view from a template)
Make the model a string on a Django HTML template
I made a command to markdown the table clipboard
Deploy a Django app made with PTVS on Azure
A Python script that allows you to check the status of the server from your browser