如何通过试用来测试扭曲的网络资源?
我正在开发一个twisted.web 服务器 - 它由一些资源组成,除了渲染内容之外,还使用adbapi 来获取一些数据并将一些数据写入postgresql 数据库。我正在尝试弄清楚如何编写一个试用单元测试,该测试单元测试将在不使用网络的情况下测试资源渲染(换句话说:这将初始化资源,为其生成虚拟请求等)。
让我们假设 View 资源是一个简单的叶子,在 render_GET 中返回 NOT_DONE_YET 并修改 adbapi 以生成简单的文本作为结果。现在,我已经编写了这段无用的代码,但我无法想出如何使其实际初始化资源并产生一些合理的响应:
from twisted.trial import unittest
from myserv.views import View
from twisted.web.test.test_web import DummyRequest
class ExistingView(unittest.TestCase):
def test_rendering(self):
slug = "hello_world"
view = View(slug)
request = DummyRequest([''])
output = view.render_GET(request)
self.assertEqual(request.responseCode, 200)
输出是... 1.我也尝试过这样的方法:output = request。 render(view) 但相同的输出 = 1。为什么?我会非常感谢一些示例如何编写这样的单元测试!
I'm developing a twisted.web server - it consists of some resources that apart from rendering stuff use adbapi to fetch some data and write some data to postgresql database. I'm trying to figoure out how to write a trial unittest that would test resource rendering without using net (in other words: that would initialize a resource, produce it a dummy request etc.).
Lets assume the View resource is a simple leaf that in render_GET returns NOT_DONE_YET and tinkers with adbapi to produce simple text as a result. Now, I've written this useless code and I can't come up how to make it actually initialize the resource and produce some sensible response:
from twisted.trial import unittest
from myserv.views import View
from twisted.web.test.test_web import DummyRequest
class ExistingView(unittest.TestCase):
def test_rendering(self):
slug = "hello_world"
view = View(slug)
request = DummyRequest([''])
output = view.render_GET(request)
self.assertEqual(request.responseCode, 200)
The output is... 1. I've also tried such approach: output = request.render(view) but same output = 1. Why? I'd be very gratefull for some example how to write such unittest!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个函数,它将渲染请求并将结果转换为渲染完成时触发的 Deferred:
它实际上在 Twisted Web 的测试套件中使用,但它是私有的,因为它本身没有单元测试。 ;)
您可以使用它来编写如下测试:
Here's a function that will render a request and convert the result into a Deferred that fires when rendering is complete:
It's actually used in Twisted Web's test suite, but it's private because it has no unit tests itself. ;)
You can use it to write a test like this:
这是一个 DummierRequest 类,它解决了我几乎所有的问题。唯一剩下的就是它没有设置任何响应代码!为什么?
Here is a DummierRequest class that fixes almost all my problems. Only thing left is it does not set any response code! Why?