Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joel Adams and Jeremy Frens Calvin College

Similar presentations


Presentation on theme: "Joel Adams and Jeremy Frens Calvin College"— Presentation transcript:

1 Joel Adams and Jeremy Frens Calvin College
Unit Testing Joel Adams and Jeremy Frens Calvin College

2 Test-First Development
Test-first development stresses the use of tests in developing software: Tests must be easy to write. Tests must be automatic. Tests must be fast. Tests must be written first. Tests must be easy to add.

3 Types of Testing Unit-testing tests the individual units (i.e., methods) of a program. Functional testing tests the interaction of these units.

4 JUnit JUnit is a open-source unit-testing framework for Java.
Written by Erich Gamma and Kent Beck as part of their development of eXtreme Programming. Uses two technologies to make testing easier: inheritance reflection

5 Test Case Class public class EmployeeTest
extends junit.framework.TestCase { // define instance variables // set up method // test methods } TestCase implements the code to automatically run the tests. The instance variables are your choice. The setup method sets up values for your instance variables. The test methods do the testing.

6 The Setup Method Must have this signature.
protected void setUp() { super.setUp(); // initialize instance variables } Must have this signature. Code should initialize your instance variables. This method is called just before each test method.

7 A Test Method All test methods must have this signature.
public void testSomething() { // run your tests } All test methods must have this signature. Replace Something with a more descriptive name. The test runners automatically find the test methods. Test methods can throw exceptions (considered a failed test).

8 Assertions TestCase provides several methods for implementing the tests: assertEquals(expected, computed); assertEquals(expected, computed, epsilon); assertTrue(booleanExpression); assertFalse(booleanExpression); assertNull(referenceExpression); fail(); Each of these methods has an optional String parameter for an output label.

9 Running Your Tests From the command line:
junit.textui.TestRunner for textual output. junit.swingui.TestRunner for GUI output (automatically reloads classes after compilation). Within an IDE: Set up IDE to run one of the command-line interfaces. Use a JUnit plug-in.

10 Interpreting the Results
“My code green barred.” The GUIs use a green bar to indicate successful tests. CLIs just print “Ok”. “My code red barred.” A red bar indicates that a test failed---a failed assertion or a thrown exception. JUnit gives you a stack trace to help in debugging.

11 Example public class EmployeeTest extends TestCase {
private Employee myEmployee1; public void setUp() { myEmployee1 = new Employee(“X”, 5.44); } public void testPaycheck() { assertEquals( 282.88, myEmployee1.getPaycheck(48), 1e-3);

12 Example … public void testBadEmployees() { try { new Employee(“”, 5);
fail(); } catch (IllegalArgumentException e) { assertTrue(e.getMessage() .matches(“name”); } // etc…


Download ppt "Joel Adams and Jeremy Frens Calvin College"

Similar presentations


Ads by Google