There are two ways unit tests can be written in Zope 3. The first one is through special TestCase classes using the unittest package, which was modeled after JUnit. The second technique is writing tests inside doc strings, which are commonly known as doc tests.

Zope 3中有两种方式进行单元测试的编写。第一种是通过专门的使用unittest包的TestCase类,这是借鉴JUnit构建的测试模型。第二种技术是在doc strings中编写单元测试,这种方式常被称为 doc tests。

Late in the development of Zope 3 doc tests became the standard way of writing tests. For philosophical and technical differences between the two approaches, see the section on “Writing Tests”, especially the “Writing Basic Unit Tests” and “Doctests: Example-driven Unit Tests” chapters.

在今后的Zope3 的开发中,文档测试将成为编写单元测试的编著方法。要了解这两种技术的详细信息,常见“编写测试”章节,特别是“编写基础单元测试”和“Doctests:案例驱动的单元测试”章节。

Common unit tests, however, are of advantage when it is desirable to reuse abstract tests, as it is the case for various container tests. Therefore, we will use unit tests for the container tests and doc tests for everything else.

通用的单元测试,在需要重用抽象测试时具有优点,因为这是各种容器测试需要的情形。因此,我们将为容器测试采用单元测试,而对任何其他东西采用文档测试。

First, create a package called tests inside the messageboard package. Note that calling the test module tests (file-based test modules would be called tests.py) is a convention throughout Zope 3 and will allow the automated test runner to pick up the tests.

首先,在messageboard包中创建一个名为tests的包。注意:Zope3中将此测试模块预定称为tests(基于文件的测试模块应当命名为tests.py),这样自动测试运行器会运行该测试。

Next, start to edit a file called test_messageboard.py and insert:

下一步,开始编辑名为test_messageboard.py的文件,并插入以下代码:

10 11 def makeTestObject(self): 12 return MessageBoard() 13 14 def test_suite(): 15 return unittest.TestSuite(( 16 DocTestSuite('book.messageboard.messageboard'), 17 unittest.makeSuite(Test), 18 )) 19 20 if name == 'main': 21 unittest.main(defaultTest='test_suite')

A lot of cool stuff just happened here. You just got your first 12 unit tests. Let’s have a closer look:

在此有许多很酷的东西第一次遇到。你只要看你的前12行的单元测试。让我们仔细看看这些代码:

Besides the container test, we also already register the doc tests.

Now it is time to do the second test module for the IMessage component. To start, we simply copied the test_messageboard.py to test_message.py and modified the new file to become:

现在,我们要为IMessage组件编写第二个测试模块。我们把test_messageboard.py拷贝成test_message.py,然后把新文件改为:

10 11 def makeTestObject(self): 12 return Message() 13 14 def test_suite(): 15 return unittest.TestSuite(( 16 DocTestSuite('book.messageboard.message'), 17 unittest.makeSuite(Test), 18 )) 19 20 if name == 'main': 21 unittest.main(defaultTest='test_suite')

There is not really any difference between the two testing modules, so that I am not going to point out the same facts again.

这两个测试模块没有任何本质的区别,因此我就不再逐点列出同样的代码说明了。

Note that none of the tests deal with implementation details yet, simply because we do not know what the implementation details will be. These test could be used by other packages, just as we used the SampleContainer base tests, since these tests only depend on the API. In general, however, tests should cover implementation-specific behavior.

注意这些测试都还没有处理具体的实现细节,因为我们我们还不知道具体的实现细节会是什么样的。就像我们使用SampleContainer基础测试件一样,这些测试件可以被其他包使用,因为这些测试仅仅依赖于API。当然,通常来说,测试应当涵盖具体实现的行为。

last edited 2005-10-08 14:07:06 by dugan