USING TESTSUITE
As mentioned earlier, TestSuites are used to collect a selection of Tests so that they can be run as a unit. Note that a TestSuite may recursively contain other TestSuites.
When you start out with JUnit you will likely be using TestSuites without even knowing it. The framework uses reflection to find and collect all of the test methods in a given TestCase whose signatures match:
public void testWhatever()
When you use this facility, you have no control over or any guarantee of the order in which the test methods will be run. Tests should be written in such a way that they are order independent, so this should not be a problem. If, however, you have a situation where you want or need tests to run in a specific order you can supply a public static Test suite() method which builds the TestSuite as required. Here is an example:
public static Test suite() { suite.addTest(new TestMovieList("testEmptyList")); suite.addTest(new TestMovieList("testAdding")); return suite; }
I can't stress enough that test methods within a TestCase should be completely independent. You should seldom have to resort to writing your own suite() method. In xUnit for languages that lack a reflection mechanism, you will have to build suites by hand, as it were.
If you find that you want or need to have test methods run in a specific order you should see if there is a way to remove that requirement. It may be that you are relying on an external resource whose state is modified by each subsequent test. Try to find a way to tease apart those dependencies, setting up the appropriate fixture individually for each test. You might be able to do this by mocking such an external resource. For more on mock objects, see Chapter 7.
Another more valid use of TestSuite, in my opinion, is to create smaller TestSuites which include a specific subset of the tests. There are two main reasons to do this:
You can create custom suites of tests that relate directly to the task you are working on. By doing this you can save time running tests by focusing the tests that are run on the area of the code you are working on. But always keep that suite and the code it tests in sync. As you find yourself needing to involve code that your suite doesn't test, be sure to add the tests for that code to your suite so that you can proceed, protected and confident.
You will most likely want to have a TestSuite that tests a complete package or module. My practice is to have a suite in each package called TestAll that includes all tests in that package and all subpackages. By doing this, I can easily test any subtree of a project. Here's an example: package com.saorsa.nowplaying.pd.tests;
import junit.framework.Test; import junit.framework.TestSuite;
public class TestAll extends TestSuite {
public TestAll() { super(); }
public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(TestMovie.class); suite.addTestSuite(TestMovieList.class); return suite; } }
|
No comments:
Post a Comment