210 likes | 309 Views
Test a Little, Code a Little. Colin Sharples IBM Global Services New Zealand. Overview. JUnit a Java instance of the Gamma/Beck xUnit framework architecture Automates unit testing Promotes a development philosophy of: “Test a Little, Code a Little”. Talk outline. Why do unit testing?
E N D
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand
Overview • JUnit • a Java instance of the Gamma/Beck xUnit framework architecture • Automates unit testing • Promotes a development philosophy of: “Test a Little, Code a Little”
Talk outline • Why do unit testing? • Framework description • Development Philosophy • Extensions • Demo
Ariane 5 Flight 501 Launched: 4 June 1996, 12:33:59 Exploded: 4 June 1996, 12:34:38
What is a unit test? • Exercises a “unit” of production code • in isolation from rest of system • checks results are as expected • A “unit” is a coherent piece of functionality • Java class or package
TestCase • Encapsulates the set of tests for one unit • public void testXxx() {} • Contains the “fixture” for multiple tests • public void setUp() {} • public void tearDown() {} • Implements the Command pattern • public void run(TestResult result) {}
Assert • Methods for checking test conditions • assertTrue() • assertNull() • assertNotNull() • assertEquals() • assertSame • fail()
TestSuite • Collects tests and test cases into suites • a suite for all the tests in a single test case • a suite for all the test cases in a package • a suite for all test cases in a component • a suite for all test cases in an application
TestResult • Gathers results from tests • failures (anticipated, check by assertions) • errors (unanticipated, e.g. runtime exceptions) • Collates all results from a single suite • Can check if an error should stop the test run
TestRunner • Provides a runtime execution environment • Runs individual tests or suites • Graphical and text based user interfaces • Swing interface • Text interface integrates with Ant • Plugin for Eclipse
Extension Points • Extend TestCase or TestSuite • ExceptionTestCase • ActiveTestSuite • Provide additional testing support • TestDecorator • TestSetup • Your extension here...
Test a little, code a little • Core philosophy • write tests first • then write the code to make the tests work • when the tests work, you’re done :-)
Unit testing saves time • Unit tests exercise the design • Unit tests give the developer confidence • Unit tests speed up integration testing
Automation is the key • Tests are useless if they are not run • JUnit allows easy automation of tests • Automatic tests ease integration testing • Automatic tests ease regression testing • Automatic tests allow installation verification
Test case organisation • Two options: • place test cases in a “test” sub-directory • place test cases in same package in a different source tree • Allow for separation of test code and production code
Example JUnit extensions • StrutsTestCase • automated testing of Jakarta Struts applications • JXUnit • provides support for data centric testing • HttpUnit • automated web site testing
Resources • www.junit.org • articles, hints & tips • downloads • www.eclipse.org • Eclipse IDE • junit plugin