Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to follow the redirct and test again in test script

I have this test script for uploading file

    with open('_material/content.xlsx','rb') as fp:
        response = self.client.login(username="user@example.com", password="qwpo1209")

        response = self.client.post('/cms/content/up',
        {'name': 'test', 'content_file': fp,"is_all":"True"})
    
        self.assertEqual(response.status_code,302) # it shows ok
        
        #then next, how can I follow the redirect and test the page??
        self.assertContains(response, "success!")

It returns the redirect 302 but I want to follow the redirect and nect check.

Because there comes the message such as success!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is it possible?

>Solution :

You can use follow=True to follow the redirect in a .post(…) call [Django-doc], so:

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

as is specified in the documentation:

If you set follow to True the client will follow any redirects and a redirect_chain attribute will be set in the response object containing tuples of the intermediate urls and status codes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading