Chapter 2 Exploring JUnit
This chapter covers:
Using the core JUnit classes
Understanding the JUnit life cycle
++++++++++++++++++++++++
As we add new classes(这里的new classes是指像Calculator这样的实际类?), we often want to make changes to classes under test(这里的classes under test是指像TestCalculator这样继承了TestCase的类?). Of course, experience has taught us that sometimes classes (这个sometimes下的classes又是指什么?像Calculator这样的实际类?)interact in unexpected ways(这个unexpected与否是以什么为标准划分的?). So, we’d really like to keep running all of our tests on all of our classes, whether they’ve been changed or not.(上面的这个unexpected后,又怎么就非得keep running all of our tests了?又有什么逻辑支持呢?) But how can we run multiple test cases? And what do we use to run all these tests?
(虽然上面这段话有疑问,但现在似乎明白了点,实际App中的类不只是有Calculator这一个,还会有其它的类(Caller)与Calculator合作,而那些类肯定也是要测试了,这样就不能只有一个TestCalculator类了,还得有像TestCaller这样测试类.而实际App中Caller与Calculator间有联系的,在测试类里TestCalculator与TestCaller也得同时运行起来.这章就是要回答这样的问题了)
2.1 Exploring Core JUnit
When you need to write more test cases, you create more TestCase objects(这是可以理解,那能不能在一个TestCase里同时没多个类呢?例如:在TestCalculator里有一个方法testAdd是没Calculator的add方法的,而另一个方法如testCall是没Caller里的cass方法呢?当然这样写名字与实测严重不符!不建议这样写,现在考虑的是JUnit能不能支持,也就是看这个FrameWork设计). When you need to run several TestCase objects at once, you create another object called a TestSuite. To run a TestSuite, you use a TestRunner, as you did for a singleTestCase object in the previous chapter. Figure 2.1 shows this trio in action.
TestCase + TestSuit + BaseTestRunner = TestResult
DEFINITION
TestCase (or test case)--A class that extends the JUnit TestCase class.
It contains one or more tests represented by testXXX methods. A test
case is used to group together tests that exercise common behaviors.
In the remainder of this book, when we mention a test, we mean a
testXXX method; and when we mention a test case, we mean a class
that extends TestCase--that is, a set of tests.
TestSuite (or test suite)--A group of tests. A test suite is a convenient
way to group together tests that are related. For example, if you don’t
define a test suite for a TestCase, JUnit automatically provides a test
suite that includes all the tests found in the TestCase (more on that
later).
TestRunner (or test runner)--A launcher of test suites. JUnit pro-
vides several test runners that you can use to execute your tests.
There is no TestRunner interface, only a BaseTestRunner that all
test runners extend. Thus when we write TestRunner we actually
mean any test runner class that extends BaseTestRunner.
These three classes are the backbone of the JUnit framework. Once you understand how TestCase, TestSuite, and BaseTestRunner work, you will be able to write whatever tests you need. On a daily basis, you only need to write test cases(那JUnit怎么知道什么时候用到TestSuit和BaseTestRunner呢?像现在的TestCalculator并没有用到这两个类--我现在的理解是TestCalculator没有用到.是有个叫testSuit.add()的方法? JUnit automatically provides a test suite that includes all the tests found in the TestCase ).The other classes work behind the scenes to bring your tests to life. These three classes work closely with four other classes to create the core JUnit framework.
2.2 Launching tests with test runners