Django: html tags for testing modules from responses and sessions

Is there a way to check the html from the answer:

response = self.client.get('/user/login/') 

I need a detailed check, for example, input identifiers and other attributes. Also, what about the sessions that were established? Is it possible to check their values ​​in the test?

+4
source share
3 answers

Not sure, but look at https://docs.djangoproject.com/en/dev/topics/testing/tools/#testing-responses .

response.context is a way to check your values.

+6
source

Simon Willison soup-select is a great way to test the content of an HTML response based on CSS selectors like jQuery. So, for example, to check that your page has an entry with the identifier my_input_id :

 from BeautifulSoup import BeautifulSoup as Soup from soupselect import select response = self.client.get('/user/login/') soup = Soup(response.content) self.assertEquals(len(select(soup, 'input#my_input_id')), 1) 
+5
source

Meticulous.

Also, what about the sessions that were established? Is it possible to check their values ​​in the test?

TDD is an externally visible behavior. To find out if a user has a session, you must specify a link that only works when the user is logged in and has a session.

A typical exercise is as follows.

 class When_NoLogin( TestCase ): def test_should_not_get_some_resource( self ): response= self.client.get( "/path/that/requires/login" ) self.assertEquals( 301, response.status_code ) 

That is, when it is not logged in, some (or all) URIs are redirected to the login page.

 class When_Login( TestCase ): def setUp( self ): self.client.login( username='this', password='that' ) def test_should_get_some_resource( self ): response= self.client.get( "/path/that/requires/login" ) self.assertContains( response, '<input attr="this"', status_code=200 ) self.assertContains( response, '<tr class="that"', count=5 ) 

https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertContains

That is, at login, some (or all) of the URIs work as expected.

In addition, the URI response contains the tags you need.

You are not testing Django to see if it is creating a session. For this, Django already has unit tests. You check the external visible behavior of your application - does it behave like a session? Are the pages displayed correctly? Are they configured correctly with session information?

+5
source

Source: https://habr.com/ru/post/1309991/


All Articles