JUnit In Action读书笔记(5)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值