如何在单元测试中使用webapp2获取uri_for?

发布于 2024-12-05 11:16:16 字数 1157 浏览 1 评论 0原文

我正在尝试使用 webapp2 对处理程序进行单元测试,但遇到了一个愚蠢的小错误。

我希望能够在测试中使用 webapp2.uri_for ,但我似乎无法做到这一点:

    def test_returns_200_on_home_page(self):
        response = main.app.get_response(webapp2.uri_for('index'))
        self.assertEqual(200, response.status_int)

如果我只是执行 main.app.get_response('/') ,它就可以工作美好的。

收到的异常是:

   Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 318, in run
    testMethod()
  File "tests.py", line 27, in test_returns_200_on_home_page
    webapp2.uri_for('index')
  File "/Users/.../webapp2_example/lib/webapp2.py", line 1671, in uri_for
    return request.app.router.build(request, _name, args, kwargs)
  File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 173, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 136, in _get_current_object
    raise RuntimeError('no object bound to %s' % self.__name__)
RuntimeError: no object bound to request

我缺少一些愚蠢的设置吗?

I'm trying to unit test a handler with webapp2 and am running into what has to be just a stupid little error.

I'd like to be able to use webapp2.uri_for in the test, but I can't seem to do that:

    def test_returns_200_on_home_page(self):
        response = main.app.get_response(webapp2.uri_for('index'))
        self.assertEqual(200, response.status_int)

If I just do main.app.get_response('/') it works just fine.

The exception received is:

   Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 318, in run
    testMethod()
  File "tests.py", line 27, in test_returns_200_on_home_page
    webapp2.uri_for('index')
  File "/Users/.../webapp2_example/lib/webapp2.py", line 1671, in uri_for
    return request.app.router.build(request, _name, args, kwargs)
  File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 173, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 136, in _get_current_object
    raise RuntimeError('no object bound to %s' % self.__name__)
RuntimeError: no object bound to request

Is there some silly setup I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-12-12 11:16:16

我认为唯一的选择是设置一个虚拟请求,以便能够为测试创建 URI:

def test_returns_200_on_home_page(self):
    // Set a dummy request just to be able to use uri_for().
    req = webapp2.Request.blank('/')
    req.app = main.app
    main.app.set_globals(app=main.app, request=req)

    response = main.app.get_response(webapp2.uri_for('index'))
    self.assertEqual(200, response.status_int)

切勿在测试之外使用 set_globals()。由 WSGI 应用程序调用,以线程安全的方式设置活动应用程序和请求。

I think the only option is to set a dummy request just to be able to create URIs for the test:

def test_returns_200_on_home_page(self):
    // Set a dummy request just to be able to use uri_for().
    req = webapp2.Request.blank('/')
    req.app = main.app
    main.app.set_globals(app=main.app, request=req)

    response = main.app.get_response(webapp2.uri_for('index'))
    self.assertEqual(200, response.status_int)

Never use set_globals() outside of tests. Is is called by the WSGI application to set the active app and request in a thread-safe manner.

朦胧时间 2024-12-12 11:16:16

webapp2.uri_for() 假设您位于 Web 请求上下文中,但由于找不到 request 对象而失败。

您可以将您的应用程序视为一个黑匣子,并使用文字 URI 来调用它,而不是解决这个问题,例如您提到的 '/' 。毕竟,您想要模拟正常的 Web 请求,并且 Web 浏览器也将使用 URI 而不是内部路由快捷方式。

webapp2.uri_for() assumes that you are in a web request context and it fails because it cannot find the request object.

Instead of working around this you could think of your application as a black box and call it using literal URIs, like '/' as you mention it. After all, you want to simulate a normal web request, and a web browser will also use URIs and not internal routing shortcuts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文