简体   繁体   English

烧瓶测试:测试应用程序请求?

[英]Flask Testing: Test App Request?

While doing the Miguel Grinberg's Flask Web Development, I got stuck while testing the gravatar code, 在做Miguel Grinberg的Flask Web开发时,我在测试gravatar代码时遇到困难,

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 is it different from app_context()? app.test_request_context()做什么以及它与app_context()的不同之处是什么?

Why do we even need to call with self.app.test_request_context('/')? 为什么我们甚至需要使用self.app.test_request_context('/')进行调用? Also, what changes can we do to shift the call to app.test_request_context() in SetUp()? 另外,我们可以做些什么更改来将调用转移到SetUp()中的app.test_request_context()?

There's plenty of reading to do on the subject, so start with the documentation: app_context , test_request_context , and you can always double-check the code: app_context and test_request_context . 关于这个主题有很多阅读,所以从文档开始: app_contexttest_request_context ,你总是可以仔细检查代码: app_contexttest_request_context In addition, here's an article discussion Flask's contexts. 另外, 这里有一篇文章讨论Flask的背景。

That's a lot of links, so for a break-down: 这是很多链接,所以要分解:

We can see that app_context creates a new application context, while test_request_context creates a new request context. 我们可以看到app_context创建了一个新的应用程序上下文,而test_request_context创建了一个新的请求上下文。 Application contexts are created in two situations : manually with app_context and when a request context is created, which, in turn, is created with test_request_context or at the beginning of the request . 应用程序上下文在两种情况下创建:手动使用app_context和创建请求上下文,而请求上下文又是使用test_request_context创建的,或者是在请求开始时创建的

So when a request comes into your application , a RequestContext is created. 因此,当请求进入您的应用程序时 ,会创建一个RequestContext The creation of this object creates an application context. 创建此对象会创建应用程序上下文。

Why test_request_context ? 为什么test_request_context You need that context to access the application when working outside of a context created by a request, like proxies that you probably recognize, like current_app , request , g , and session . 在请求创建的上下文之外工作时,您需要该上下文来访问应用程序,例如您可能识别的代理 ,例如current_apprequestgsession Going down into the code, when you create a RequestContext with test_request_context instead of request_context , you're getting a EnvironBuilder object . 深入到代码中,当您使用test_request_context而不是request_context创建EnvironBuilder ,您将获得一个EnvironBuilder对象

Check out tbicr 's answer here. 在这里查看tbicr的答案。

Specifically, this snippet of code 具体来说,这段代码

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

requires request context since it needs to access 'request' variable. 需要请求上下文,因为它需要访问'request'变量。

The definition of gravatar method in User Model needs '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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM