论坛首页 综合技术论坛

JUnit In Action读书笔记(5)

浏览 1604 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-01   最后修改:2010-11-15

2.3.2 Rolling your own test suite

The default TestSuite goes a long way toward keeping the simple things simple.But what happens when the default suite doesn’t meet your needs? You may want to combine suites from several different packages as part of a master suite. If you’re working on a new feature, then as you make changes, you may want to run a small set of relevant tests.(深层次解耦合,高度可配置性,佩服!)

Design patterns in action: Composite and Command Composite pattern.  "Compose objects into tree structures to represent partwhole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. JUnit’s use of the Test interface to run a single test or suites of suites of suites of tests is an example of the Composite pattern.When you add an object to a TestSuite, you are really adding a Test, not simply a TestCase. Because both TestSuite and TestCase implement Test, you can add either to a suite. If the Test is a TestCase, the single test is run. If the Test is a TestSuite, then a group of tests is run (which could include other TestSuites).

Command pattern.  "Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations." The use of the Test interface to provide a common run method is an example of the Command pattern.



2.4 Collecting parameters with TestResult
    Newton taught that...
    Likewise,for every TestSuite,there is a TestResult.

    for "assertEquals(60,result,0);", if the result did not equal 60, JUnit would create a TestFailure object to be stored in the TestResult.

    JUnit distinguishes between failures and errors.Failure are to be expected.From time to time changes in the code (这个changes in the code具体是指?这个changes仅仅指代码层面上的错误?像拼写出错?)will cause an assertion to fail.When it does,you fix the code so the failure goes away. An error is an unexpected condition that is not expected by the test, such as an exception in a conventional program.

    Of course, an error may indicate a failure in the underlying environment.The test itself may not be broken.A good heuristic in the face of an error is the following:
        1,Check the environment.(is the database up? what about the network?)
        2,check the test.
        3,check the code.
    The TestResult is used by almost all the JUnit classes internally. As a JUnit test writer, you won’t interact with the TestResult directly; but in trying to understand how JUnit works, it’s helpful to know that it exists.    

    Design patterns in action: Collecting Parameter "When you need to collect results over several methods, you should add a parameter to the method and pass an object that will collect the results for you."  The TestResult class is an example of the Collecting Parameter pattern.

2.5 Observing results with TestListener

论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics