在 Python 中对 Corba 进行单元测试
我对您对使用 Corba 与服务器通信的单元测试代码的看法感兴趣。
你会嘲笑 Corba 对象吗?
谢谢!
笔记: 我相信我还没有说得足够清楚,所以我将尝试给出一个更具体的示例:
Web 应用程序需要显示一个包含从服务器接收的数据的页面。它通过调用 server_pagetable.getData() 获取数据,然后格式化数据,将它们转换为正确的 Python 类型(因为 Corba 没有日期类型等),最后创建要显示的 HTML 代码。
这就是我想要测试的 - 接收数据并进行所有转换并最终创建 HTML 代码的方法。
我相信最直接的决定是模拟 Corba 对象,因为它们本质上包含网络和数据库功能(不应在单元测试中进行测试)。
只是这是相当多的“额外工作”要做 - 模拟所有 Corba 对象(有一个 User 对象、一个服务器会话对象、页表对象、一个管理对象等)。也许只是因为我被 Corba 困住了,因此我必须用模拟来反映服务器规定的对象层次结构。另一方面,可能有一些很酷、优雅的解决方案可以使用 Corba 测试代码,但我并没有想到。
I am interested in your opinions on unit-testing code that uses Corba to communicate with a server.
Would you mock the Corba objects?
Thanks!
Note:
I believe I have not made myself clear enough, so I'll try to give a somewhat more concrete example:
A web application needs to display a page containing data received from the server. It obtains the data by calling server_pagetable.getData() and then formats the data, converts them to the correct Python types (because Corba does not have e.g. a date type, etc.) and finally creates the HTML code to be displayed.
And this is what I would like to test - the methods that receive the data and do all the transformations and finally create the HTML code.
I believe the most straightforward decision is to mock the Corba objects as they essentially comprise both the networking and db functionality (which ought not to be tested in unit tests).
It's just that this is quite a lot of "extra work" to do - mocking all the Corba objects (there is a User object, a server session object, the pagetable object, an admin object etc.). Maybe it's just because I'm stuck with Corba and therefore I have to reflect the object hierarchy dictated by the server with mocks. On the other hand, it could be that there is some cool elegant solution to testing code using Corba that just did not cross my mind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要尝试对 Corba 进行单元测试。假设 Corba 有效。对您自己的代码进行单元测试。这意味着:
创建一个单元测试,检查您是否正确设置 Corba 以及您是否可以调用单个方法并读取属性。如果有效,所有其他方法和属性也将有效。
之后,测试所有暴露的对象是否正常工作。为此,您不需要 Corba。
Don't try to unittest Corba. Assume that Corba works. Unittest your own code. This means:
Create a unit test which checks that you correctly set up Corba and that you can invoke a single method and read a property. If that works, all other methods and properties will work, too.
After that, test that all the exposed objects work correctly. You don't need Corba for this.
我会设置一个测试服务器,并对其进行实时测试。对于网络内容进行单元测试可能会很棘手,因此最好尽可能保持真实。任何模拟都将在测试服务器上完成,例如,如果您需要与三个不同的服务器进行通信,则可以使用三个不同的 IP 地址来设置它来扮演所有三个服务器的角色。
I would set up a test server, and do live tests on that. Unittesting can be tricky with network stuff, so it's best to keep it as real as possible. Any mocking would be done on the test server, for instance if you need to communicate to three different servers, it could be set up with three different IP addresses to play the role of all three servers.
如果您想在两者之间测试各个函数,那么我将获得模拟数据(本质上仍然是数据,因此如果可能的话,您可以生成模拟数据而不是模拟类)。仅当您没有完整代码的某些部分并且这些函数需要代码的这些部分的一些输入,但由于您对它们不感兴趣或没有它们时,您才能简化与它们的交互,才会使用模拟。
我有类似的工作需要处理,但我可能不会为 CORBA 对象或更具体的 COM 对象(CORBA 的实现)的实现编写测试。我必须为使用这些结构而不是结构本身的工作编写测试(尽管如果我问太多问题,我也可能会担任这个角色)。
归根结底,单元测试是较小规模的集成,因此每当我编写测试时,我总是考虑输入和输出而不是实际结构。从你写问题的方式来看,我的注意力集中在 server_pagetable.getData() 的数据和输出 HTML 上,而不关心太多之间发生的事情(因为这是你正在测试的代码,你不希望定义测试中的代码但确保输出正确)。
If you want to test individual functions in between then I would get mock data (essentially still data, so you can generate mock data rather than mock class if possible). Mocks are only used when you don't have parts of the full code and those functions need some input from those parts of the code but as you are not interested in them or don't have them you simplify the interaction with them.
I have similar work to tackle but I probably will not write a test for the implementation of CORBA objects or more specifically COM objects (implementation of CORBA). I have to write tests for work that uses these structures as opposed to the structures themselves (although I could land myself in that role too if I ask too many questions).
At the end of the day, unit test is integration on a smaller scale so whenever I write tests I am always thinking of input and outputs rather than actual structures. From the way you have written your problem, my concentration would be on the data of server_pagetable.getData() and the output HTML without caring too much about what happens in between (because that is the code you are testing, you don't want to define the code in the test but ensure that output is correct).