Home > Blockchain >  What is equivalent to TestCase.client in normal script
What is equivalent to TestCase.client in normal script

Time:03-23

For example with TestCase I can check login post and so on with self.client

class TestMyProj(TestCase):
   response = self.client.login(username="[email protected]", password="qwpo1209")

   response = self.client.post('/cms/content/up',
        {'name': 'test', '_content_file': fp},
        follow=True)

However now I want to use this in script not in test case.

because this is very useful to make initial database.

I want to do like this.

def run():
   response = client.login(username="[email protected]", password="qwpo1209")
   response = client.post('/cms/content/up',
        {'name': 'test', '_content_file': fp},
        follow=True)

What is equivalent to TestCase.client in normal script??

CodePudding user response:

Generally I recommend to create command to make initial database instead of using the Client class that we can use to test the application. https://docs.djangoproject.com/en/4.0/howto/custom-management-commands/ You can also take advantage of bulk methods to create many instances in a single query.

CodePudding user response:

It is a Client object [Django-doc], so:

from django.test.client import Client

def run():
    client = Client()
    response = client.login(username="[email protected]", password="qwpo1209")
    response = client.post(
        '/cms/content/up',
        {'name': 'test', '_content_file': fp},
        follow=True
    )

The documentation discusses the parameters that can be passed when constructing a Client object.

  • Related