JUnit In Action读书笔记(4)

2.2 Launching tests with test runners
    Writing tests can be fun, but what about the grunt work of running them?
2.2.1 Selecting a test runner
    The JUnit distribution includes three TestRunner classes: one for the text console, one for Swing, and even one for AWT (the latter being a legacy that few people still use).
2.2.2 Defining your own test runner
    you could also extend this class yourself.
2.3 Composing tests with TestSuite
    ............
    Altogether, this seems simple--at least as far as running a single test case is concerned.

    But what happens when you want to run multiple test cases?Or just some of your test cases?How can you group test cases?

     JUnit’s answer to this puzzle is the TestSuite. The TestSuite is designed to run one or more test cases. The test runner launches the TestSuite; which test cases to run is up to the TestSuite.

2.3.1 Running the automatic suite
    To keep simple things simple, the test runner automatically creates a TestSuite if you don’t provide one of your own. (Sweet!)

    The default TestSuite scans your test class for any methods that start with the characters test. Internally, the default TestSuite creates an instance of your TestCase for each testXXX method. (原来是要为每一个testXXX method都新create个instance呀!佩服!!).The name of the method being invoked is passed as the TestCase constructor, so that each instance has a unique identity.
    
    For the TestCalculator, the default TestSuite could be represented in code like this:
    public static Test suite(){
          return new TestSuite(TestCalculator.class);  // (2)
    }
    (这里的并没有体现出把要测方法的名字传入TestCase的constructor里面当作unique identify吧?倒是下面的(1)处用了"testAdd",难道说JUnit能自动检测出上面的(2)处是想测"add"方法?不对吧.....)

    And this is again equivalent to the following:
      public static Test suite()
      {
          TestSuite suite = new TestSuite();
          suite.addTest(new TestCalculator("testAdd")); // --(1)
          return suite;
      }

      the automatic test suite ensures that you don’t forget to add some test to the test suite.

2.3.2 Rolling your own test suite

猜你喜欢

转载自rmn190.iteye.com/blog/178149