160 likes | 308 Views
EMOOSE 2001-2002 Object-Oriented Software E volution. Extreme Programming. Dr. Tom Mens tom.mens@vub.ac.be Programming Technology Lab http://prog.vub.ac.be Vrije Universiteit Brussel Pleinlaan 2 - 1050 Brussel Belgium. Characteristics of XP. Pair Programming Unit Testing Refactoring
E N D
EMOOSE 2001-2002Object-Oriented Software Evolution Extreme Programming Dr. Tom Mens tom.mens@vub.ac.be Programming Technology Lab http://prog.vub.ac.be Vrije Universiteit Brussel Pleinlaan 2 - 1050 Brussel Belgium
Characteristics of XP • Pair Programming • Unit Testing • Refactoring • Write tests before you implement new functionality or refactor the code • Run tests after the change to check correctness
Testing: Definitions • Regression testing • selective retesting of a system or component to verify that modifications have not caused unintended effects and that the system or component still complies with its specified requirements • Unit testing • Tests are associated to individual hardware or software units or groups of related units • the tests identify clearly which part of the system they thest • E.g., a particular class, method or package • IEEE Standard 1990 • Institute of Electrical and Electronics Engineers, Standard Computer Dictionary: A Compilation of IEEE Standard Computer Glossaries. NewYork, NY: 1990
Unit Testing • TestCase • A collection of tests for a given unit (e.g., a class) • Each test is implemented as an ordinary Java method without parameter and return type • TestSuite • A collection of test cases, one for every relevant unit • Testing frameworks available for all main OO languages • Java: JUnit (www.JUnit.org) • Smalltalk: SUnit • C++ • Perl
JUnit Class Hierarchy Object TestSuite Assert addTest addTestSuite run assertEquals assertNotNull assertNull assertSame assertTrue run Composite design pattern:TestSuite bundles a set of TestCases and TestSuites <<interface>> Test TestCase All errors and failures are collected in a TestResult TestResult setUp tearDown run runTest run countTestCases addError addFailure run
JUnit sequence diagram :TestRunner :TestSuite tc:TestCase tr:TestResult run(tr) run(tr) run(tc) runBare() setUp() runTest() tearDown() addFailure()
Unit Testing in Java:TestCase Example import junit.framework.TestCase; public class PacketTest extends TestCase { private Packet p1, p2; private Node n; public PacketTest(String arg0) { super(arg0); } //constructor protected void setUp() throws Exception { super.setUp(); n = new Node("Node 1"); p1 = new Packet(”contents 1", n); p2 = new Packet(”contents 2", n); // same addressee as p1 } public void testPacketContents() { assertNotSame(p1,p2); } protected void tearDown() throws Exception { super.tearDown(); } public static void main(String args[]) { junit.swingui.TestRunner.run(PacketTest.class); } }
Unit Testing in Java:TestSuite Example import junit.framework.Test; import junit.framework.TestSuite; public class LANTests { public static void main(String[] args) { junit.swingui.TestRunner.run(LANTests.class); } public static Test suite() { TestSuite suite = new TestSuite("Test for LAN"); suite.addTest(new TestSuite(NodeTest.class)); suite.addTest(new TestSuite(PacketTest.class)); suite.addTest(new TestSuite(PrintserverTest.class)); suite.addTest(new TestSuite(WorkstationTest.class)); return suite; } }
Unit Testing in JavaJUnit • JUnit (3.8.1) • Different user interfaces available • Swing, AWT, text • Hierarchical view on the test suites • Plug-in for Eclipse • Transparently integrated into the Eclipse IDE • Support for tracing of test failures
Unit Testing in Java:Dealing with Exceptions TestCase setUp tearDown run runTest Exception ExceptionTestCase In junit.extensions addTest addTestSuite run runTest Application-specific code MyException MyExceptionTest
Unit Testing in Java:ExceptionTestCase Example import junit.extensions.ExceptionTestCase; public class CyclingTest extends ExceptionTestCase { private Packet p; private Workstation w; private Printserver s; public CyclingTest(String a0, Class a1) { super(a0,a1); } //constructor protected void setUp() throws Exception { // create 2-node network with unknown-destination packet super.setUp(); w = new Workstation("Workstation"); s = new Printserver(”Printserver", w); w.setNextNode(s); p = new Packet(”blabla", new Node("Node"); } public void testCycling() throws UnknownDestination { p.originator = w; w.send(p); // this throws an UnknownDestination } }
Unit Testing in Java:ExceptionTestCase Example import junit.framework.Test; import junit.framework.TestSuite; public class LANTests { public static void main(String[] args) { junit.swingui.TestRunner.run(LANTests.class); } public static Test suite() { TestSuite suite = new TestSuite("Test for LAN"); suite.addTest(new TestSuite(NodeTest.class)); … suite.addTest(new CyclingTest( "testCycling", UnknownDestination.class)); return suite; } }
Unit Testing in JavaExceptions alternative solution import junit.framework.TestCase; public class WorkstationTest extends TestCase { private Workstation w; private Printserver s; private Packet p; public WorkstationTest(String arg0) { super(arg0); } //constructor protected void setUp() throws Exception { super.setUp(); w = new Workstation("Workstation"); s = new Printserver(”Printserver", w); w.setNextNode(s); p = new Packet(”contents", new Node("Node"); } public void testOriginator() {…} public void testCycling() { try { p.originator = w; w.send(p); } catch (UnknownDestination e) {return;} fail("I expected an UnknownDestination here…"); } public static void main(String args[]) { junit.swingui.TestRunner.run(PacketTest.class); } }
References • K. Beck, E. Gamma. Test Infected: Programmers Love Writing Tests. JUnit Documentation, 1998 • K. Beck. Extreme Programming Explained: Embrace Change. Addison Wesley, 2000