单元测试时如何获取实际的 Pyramid 请求 [英] How to get an actual Pyramid request when unit testing

查看:58
本文介绍了单元测试时如何获取实际的 Pyramid 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Pyramid 应用程序,我仍在努力学习.我应该为它编写单元测试,但我不知道如何构建请求.

I have a Pyramid app that I'm still trying to learn. I'm supposed to write unit tests for it but I don't know how to build a request.

我看到 Pyramid 有一个带有 DummyRequest 的测试模块,但这是空白的,很明显,如果我将它传递到视图中,它将失败,因为它没有填充请求时具有的属性运行.

I see that Pyramid has a test module with a DummyRequest but this is blank and obviously if I pass that into the views it will fail because it's not populated with the attributes that the request would have while running.

那么问题是,如何在测试时传递看起来像运行时请求的请求?

So the question is, how to I pass a request that looks like a run-time request while testing?

推荐答案

每当您进行单元测试(这与功能测试不同)时,要意识到的是您正在测试一个小的单元".该单元(在本例中为您的视图)不需要真正的"请求,也不需要完全工作的系统.该视图对自称为请求"的对象可能具有的内容有一定的期望,但仅此而已,并且它当然不需要真实请求中可用的所有内容.这就是模拟或虚拟对象发挥作用的地方.您想测试您的视图,以便您可以将一些东西传递到您的视图中,它需要检查它是否在做这项工作.假设您有以下配置:

The thing to realize whenever you are unit testing (which is different from functional tests) is that you are testing a small "unit". This unit (your view in this case) does not require a "real" request, nor does it require a fully working system. That view has certain expectations of what an object calling itself a "request" may have, but that's about it, and it certainly doesn't require everything available in a real request. This is where mocking, or dummy objects come into play. You want to test your view, so you can pass something into your view with the properties it needs to check if it's doing the job. Let's say you have the following configuration:

def main():
    config = Configurator()
    config.add_route('user', '/users/{uid}')
    return config.make_wsgi_app()

@view_config(route_name='user', renderer='user_template.mako')
def user_view(request):
    uid = request.matchdict['uid']
    user = find_user(request, uid)
    if user is None:
        raise HTTPNotFound
    return {'user': user}

def find_user(request, uid):
    return request.db.query(User).filter_by(id=uid).first()

太好了,所以这是一个真实的视图,您会注意到它只需要请求具有 2 个属性,matchdictdb.好吧,我们可以这样做:

Great, so this is a real view and you'll notice that it only requires the request to have 2 attributes, matchdict and db. Well we can do that:

class Test_user_view(unittest.TestCase):
    def test_it(self):
        req = DummyRequest()
        req.db = DummyDB()
        req.matchdict = {'uid': '3'}
        result = user_view(req)
        self.assertEqual(result['user'].id, 3)

现在我们没有解决的一件事是 DummyDB 的实现,但更好的方法可能是模拟 find_user 以返回一个虚拟值.这使测试保持简单并专注于视图本身,而不会陷入与数据库的对话.这是一个单独的测试.

Now the one thing we don't address here is the implementation of DummyDB but a better approach might be to mock out find_user to return a dummy value. This keeps the test simple and focused on the view itself without getting bogged down in talking to the database. That's a separate test.

此处的其他答案更全面地涵盖了功能测试,您肯定应该考虑使用 WebTest 来帮助确保您的整个应用程序按预期工作,但这不是单元测试的领域.

Other answers here cover functional testing more thoroughly and you should certain look at using WebTest to help with ensuring that your entire application is working as you expect it to, but this is not the realm of a unit test.

这篇关于单元测试时如何获取实际的 Pyramid 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆