Java Reference
In-Depth Information
Test fixtures
The previous example is simple, but not all tests are so straightforward. For
example, if a series of tests requires some amount of setup, you can override the
setUp() method of TestCase . This method is called immediately before each test
method, followed by a call to tearDown() , which can be overridden to perform
any necessary cleanup. This shared setup code is known as a test fixture .
The benefit of using test fixtures is best illustrated with an example. Say you're
testing code responsible for managing a message board or discussion forum.
You've determined that a series of tests is required to verify that you can manipu-
late postings appropriately, such as changing the subject or author. Each of these
operations can be tested by a single test method, but they all must act on an exist-
ing message. In this case, a test fixture is the best way to prepare the system for
the test methods. The setUp() method is responsible for creating a new posting,
and the tearDown() method is used to delete the test message from the system fol-
lowing each test method.
Test suites
Related test cases are grouped into logical collections known as test suites . When
running your unit tests, you can run not only individual test cases, but test suites
as well. Generally, you'll want to have a single test suite that relates all your unit
tests together so you can run them in a single operation. Additional hierarchy is
possible, because test suites can include other test suites. By combining groups of
related tests into suites, you can avoid running the entire test suite if you just want
to test one area of the code. For example, you may group all the security-related
tests separately from your database tests, allowing you to spot test certain areas of
your program.
Test runners
An application that knows how to interpret and run JU nit test cases is a test runner .
Although JU nit ships with its own simple test runner, as does Ant, IDEA includes
an integrated test runner with many more features. We'll discuss this in detail in
the next section.
7.2 Adding test cases to your project
IDEA doesn't provide the ability to create new JU nit test case classes explicitly,
but it's easy to add this capability through IDEA 's file templates feature. As dis-
cussed in chapter 12, file templates allow you to create a starting point for new
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search