Flask testing: test application?

While developing Miguel Grinberg Flask Web Development, I got stuck while testing gravatar code,

def test_gravatar(self): u = User(email=' john@example.com ', password='cat') with self.app.test_request_context('/'): gravatar = u.gravatar() gravatar_256 = u.gravatar(size=256) gravatar_pg = u.gravatar(rating='pg') gravatar_retro = u.gravatar(default='retro') with self.app.test_request_context('/', base_url='https://example.com'): gravatar_ssl = u.gravatar() self.assertTrue('http://www.gravatar.com/avatar/' + 'd4c74594d841139328695756648b6bd6'in gravatar) self.assertTrue('s=256' in gravatar_256) self.assertTrue('r=pg' in gravatar_pg) self.assertTrue('d=retro' in gravatar_retro) self.assertTrue('https://secure.gravatar.com/avatar/' + 'd4c74594d841139328695756648b6bd6' in gravatar_ssl) 

What does app.test_request_context () do and how does it differ from app_context ()?

Why do we need to call using self.app.test_request_context ('/')? Also, what changes can we make to translate the call in app.test_request_context () to SetUp ()?

+6
source share
2 answers

There is a lot of reading here, so start with the documentation: app_context , test_request_context , and you can always double check the code: app_context and test_request_context . In addition, the context of the flack is discussed here .

These are many links, so for the breakdown:

We see that app_context creates a new application context, and test_request_context creates a new request . Application contexts are created in two situations : manually with app_context and when creating a request context, which, in turn, is created using test_request_context or at the beginning of the request .

Therefore, when a request comes into your application , a RequestContext is created. Creating this object creates an application context.

Why test_request_context ? This context is needed to access the application when working outside the context created by the request, for example proxies , which you probably recognize, for example current_app , request , g and session . If you create a RequestContext with test_request_context instead of request_context , you get an EnvironBuilder object .

+4
source

Pay attention to tbicr .

In particular, this piece of code

 gravatar = u.gravatar() gravatar_256 = u.gravatar(size=256) gravatar_pg = u.gravatar(rating='pg') gravatar_retro = u.gravatar(default='retro') 

requires a request context, because it needs to access the query variable.

The definition of the gravatar method in the User Model needs the request variable.

 def gravatar(self, size=100, default='identicon', rating='g'): if request.is_secure: # here url = 'https://secure.gravatar.com/avatar' else: url = 'http://www.gravatar.com/avatar' hash = self.avatar_hash or hashlib.md5(self.email.encode('utf-8')).hexdigest() return '{url}/{hash}?s={size}&d={default}&r={rating}'.format(url=url, hash=hash, size=size, default=default, rating=rating) 
0
source

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


All Articles