个人工具
登录
查看“Zope3宝典/使用XML-RPC访问”的源代码 - Ubuntu中文
页面
讨论
查看源代码
历史
搜索
导航
首页
最近更改
随机页面
页面分类
帮助
编辑
编辑指南
沙盒
新闻动态
字词处理
工具
链入页面
相关更改
特殊页面
页面信息
查看“Zope3宝典/使用XML-RPC访问”的源代码
来自Ubuntu中文
←
Zope3宝典/使用XML-RPC访问
跳转至:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
=== 23.2 Step II: Testing(23.2 步骤 II: 测试) === Of course, the testing code is multiples more complex than the actual implementation, since we have to bring up the component architecture and the event service manually. Similar to the implementation, we can again separate the container-related tests in a base class (the code should be located in tests/test_xmlrpc.py):<br>当然,测试代码相对实际实现来说是成倍复杂的,因为我们不得不手工提出组件架构和事件服务。与实现相同,我们可以在基类中分离出容器相关的测试(代码被放置在 tests/test_xmlrpc.py 中): <pre><nowiki>#!python from zope.app import zapi from zope.app.tests.placelesssetup import PlacelessSetup class MessageContainerTest(PlacelessSetup): def _makeMethodObject(self): return NotImplemented def _makeTree(self): methods = self._makeMethodObject() msg1 = Message() msg1.title = 'Message 1' msg1.description = 'This is Message 1.' msg2 = Message() msg2.title = 'Message 1' msg2.description = 'This is Message 1.' methods.context['msg1'] = msg1 methods.context['msg2'] = msg2 return methods def test_getMessageNames(self): methods = self._makeTree() self.assert_(isinstance(methods.getMessageNames(), list)) self.assertEqual(list(methods.context.keys()), methods.getMessageNames()) def test_addMessage(self): methods = self._makeTree() self.assertEqual(methods.addMessage('msg3', 'M3', 'MB3'), 'msg3') self.assertEqual(methods.context['msg3'].title, 'M3') self.assertEqual(methods.context['msg3'].body, 'MB3') def test_deleteMessage(self): methods = self._makeTree() self.assertEqual(methods.deleteMessage('msg2'), True) self.assertEqual(list(methods.context.keys()), ['msg1']) </nowiki></pre> * Line 6-7: The implementation of this method should return a valid XML-RPC method publisher.<br>第 6-7 行: 该方法的实现将返回一个合法的 XML-RPC 方法发布 * Line 9-19: Create an interesting message tree, so that we have something to test with.<br>第 9-19 行: 创建一个兴趣消息树,以便我们可以用它来进行测试。 * Line 21-25: Make sure the names list is converted to a Python list and all elements are contained in it.<br>第 21-25 行: 确保名称列表被转换成 Python 列表且所有元素都被包含在其中。 * Line 27-32: This method obviously tests the adding capability. We just try to make sure that the correct attributes are assigned to the message.<br>第 27-32 行: 显然该方法测试新加的功能。我们试图确保正确的属性被指定到消息。 * Line 34-37: Simply checks that a message is really deleted.<br>第 34-37 行: 简单检查消息确实被删除了。 Now that we have the base class, we can implement the real test cases and add tests for the property accessors and mutators:<br>现在我们有了基类,我们可以实现真实测试代码并为 accessors 和 mutators 的属性添加测试。 <pre><nowiki>#!python import unittest from zope.publisher.xmlrpc import TestRequest from book.messageboard.message import Message from book.messageboard.messageboard import MessageBoard from book.messageboard.xmlrpc import MessageBoardMethods, MessageMethods class MessageBoardMethodsTest(MessageContainerTest, unittest.TestCase): def _makeMethodObject(self): return MessageBoardMethods(MessageBoard(), TestRequest()) def test_description(self): methods = self._makeTree() self.assertEqual(methods.getDescription(), '') self.assertEqual(methods.setDescription('Board 1') , True) self.assertEqual(methods.getDescription(), 'Board 1') class MessageMethodsTest(MessageContainerTest, unittest.TestCase): def _makeMethodObject(self): return MessageMethods(Message(), TestRequest()) def test_title(self): methods = self._makeTree() self.assertEqual(methods.getTitle(), '') self.assertEqual(methods.setTitle('Message 1') , True) self.assertEqual(methods.getTitle(), 'Message 1') def test_body(self): methods = self._makeTree() self.assertEqual(methods.getBody(), '') self.assertEqual(methods.setBody('Body 1') , True) self.assertEqual(methods.getBody(), 'Body 1') def test_suite(): return unittest.TestSuite(( unittest.makeSuite(MessageBoardMethodsTest), unittest.makeSuite(MessageMethodsTest), )) if <u>name</u> == '<u>main</u>': unittest.main(defaultTest='test_suite') </nowiki></pre> * Line 11-12 & 22-23: Create a XML-RPC method publisher for the message board and the message, respectively. To do that we need an object instance (no problem) and an XML-RPC request. Luckily, like for the browser publisher, the XML-RPC publisher provides a TestRequest which was written for its easy usage in unit tests like these.<br>第 11-12 & 22-23 行: 为留言薄和消息各创建一个 XML-RPC 方法发布,要做到这点我们需要一个对象实例(没问题)和一个 XML-RPC 请求。幸运的是,与浏览器发布相似,XML-RPC发布也提供了一个为在单元测试中能方便使用的 TestRequest。 * Line 38-45: And again the usual unit test boiler plate.<br>And again the usual unit test boiler plate. The rest of the code is not so interesting and should be obvious to the reader. Please run these tests now and make sure that everything passes.<br>代码的其他部分显然不那么会引起读者的兴趣。现在请运行这些测试并确保通过。
返回至
Zope3宝典/使用XML-RPC访问
。