Python - 扭曲和单元测试
我正在为作为 HTTP 服务器运行的应用程序的一部分编写单元测试。我一直尝试采取的方法是导入包含 HTTP 服务器的模块,启动它。然后,单元测试将使用 urllib2 进行连接、发送数据并检查响应。
我们的 HTTP 服务器使用 Twisted。这里的一个问题是我对 Twisted 不太熟悉:)
现在,我实例化我们的 HTTP 服务器并在 setUp() 方法中启动它,然后在 TeaDown() 方法中停止它。
问题是,Twisted 似乎不喜欢这样,它只会运行一个单元测试。第一个之后,反应堆将不再启动。
我一直在寻找、寻找、寻找,但似乎找不到有意义的答案。
我是否完全采用了错误的方法,或者只是错过了一些明显的东西?
I'm writing unit tests for a portion of an application that runs as an HTTP server. The approach I have been trying to take is to import the module that contains the HTTP server, start it. Then, the unit tests will use urllib2 to connect, send data, and check the response.
Our HTTP server is using Twisted. One problem here is that I'm just not that familiar with Twisted :)
Now, I instantiate our HTTP server and start it in the setUp() method and then I stop it in the tearDown() method.
Problem is, Twisted doesn't appear to like this, and it will only run one unit test. After the first one, the reactor won't start anymore.
I've searched and searched and searched, and I just can't seem to find an answer that makes sense.
Am I taking the wrong approach entirely, or just missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是一些信息:使用试用版为 Twisted 代码编写测试
您还应该查看在审判命令的帮助下。试用中有很多好东西!但在异步应用程序中进行测试并不总是那么容易。祝你好运!
Here's some info: Writing tests for Twisted code using Trial
You should also look at the -help of the trial command. There'a lot of good stuff in trial! But it's not always easy to do testing in a async application. Good luck!
我相信,对于 Twisted 中的单元测试,您应该使用 TwistedTrial (它是一个核心组件,即,在扭曲/试用目录中随扭曲 tarball 一起提供)。然而,正如我指出的 URL 所说,该文档主要是通过查看源代码(包括各种 Twisted 项目的源代码,因为它们也经过了 Trial 测试)。
I believe that for unit testing within Twisted you're supposed to use TwistedTrial (it's a core component, i.e., comes with the Twisted tarball in the twisted/trial directory). However, as the URL I've pointed to says, the doc is mostly by having a look through the source (including sources of various Twisted projects, as they're tested with Trial too).
正如其他人提到的,您应该在 Twisted 中使用 Trial 进行单元测试。
您还应该自下而上进行单元测试 - 这就是单元测试中“单元”的含义。在测试界面之前测试您的数据和逻辑。对于 HTTP 接口,您应该使用模拟请求调用 processGET、processPOST 等,但只有在测试了这些方法所调用的内容之后才应该执行此操作。每个测试都应该假设其他地方测试的单元按设计工作。
如果您使用的是 HTTP,或者您需要正在运行的服务器或其他状态,您可能会进行更高级别的测试,例如功能或集成测试。这并不是一件坏事,但您可能需要重新表述您的问题。
As others mentioned, you should be using Trial for unit tests in Twisted.
You also should be unit testing from the bottom up - that's what the "unit" in unit testing implies. Test your data and logic before you test your interface. For a HTTP interface, you should be calling processGET, processPOST, etc with a mock request, but you should only be doing this after you've tested what these methods are calling. Each test should assume that the units tested elsewhere are working as designed.
If you're speaking HTTP, or you need a running server or other state, you're probably making higher level tests such as functional or integration tests. This isn't a bad thing, but you might want to rephrase your question.
Twisted 存在已知错误(可能无法修复),需要重新启动反应堆导致坠毁。
这就是您的单元测试不起作用的原因。
除了使用试用之外,您可能还需要考虑单独的测试系统像客户端一样与您的 HTTP 服务器对话。
There is a known bug with Twisted (that probably won't get fixed) where re-starting the reactor causes a crash.
This is why your unit tests don't work.
As well as using Trial you might want to consider seperate testing systems that talk to your HTTP server like a client will.