16
Unit Testing with JUnit Dan Fleck Spring 2010

Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Embed Size (px)

Citation preview

Page 1: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Unit Testing with JUnit

Dan FleckSpring 2010

Page 2: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,
Page 3: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

What is Unit Testing?

A procedure to validate individual units of Source Code

Example: A procedure, method or class

Validating each individual piece reduces errors when integrating the pieces together later

Page 4: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Automated Unit Tests with JUnitJunit is a unit testing

framework for Java Allows you to write unit tests

in Java using a simple interface

Automated testing enables running and rerunning tests very easily and quickly

Page 5: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

An example unit test

@Testpublic void testCellChangePropagates() {

Spreadsheet sheet = new Spreadsheet();

sheet.put("A1", "5"); sheet.put("A2", "=A1"); sheet.put("A1", "10");

assertEquals("10",sheet.get("A2")); }

Page 6: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Junit Assert During a test use Asserts to specify if the test passed or failed

org.junit.Assert – allows you to test if certain ideas hold by asserting results: http://junit.sourceforge.net/javadoc/

assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) assertEquals(message, expected, actual, delta) assertFalse(condition) assertFalse(message, condition) Assert(Not)Null(object) Assert(Not)Null(message, object) Assert(Not)Same(expected, actual) Assert(Not)Same(message, expected, actual) assertTrue(condition) assertTrue(message, condition)

Page 7: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Junit Methods – Java annotations @BeforeClass // Run before all tests in class public static void setUpClass() throws Exception {}

@AfterClass // Run after all tests in class public static void tearDownClass() throws Exception {}

@Before // Run before each test in class public void setUp() {}

@After // Run after each test in class public void tearDown() {}

@Test public void testMain() { http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/

Page 8: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Junit with Netbeans

1. New File2. Choose file type: Junit3. Choose Test for Existing Class4. Junit test with all stubs

created for that class5. Fill in the individual tests6. Run Tests (Netbeans options)

Note: If this option doesn’t exist use New File->Other->Junit->Test for existing class

Page 9: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Junit Documentation/Tutorials http://junit.sourceforge.net/

http://code.google.com/p/t2framework/wiki/JUnitQuickTutorial

http://junit.sourceforge.net/doc/testinfected/testing.htm (older)

Page 10: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Lines of code coverage analysis

Determining which lines of code your tests have exercised and which they have not

Allows you to detect if your unit tests (or system tests) are adequately covering all possibilities or not

This is just one way to test

Page 11: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Lines of code coverage analysis with Netbeans• Install Unit Test Code

Coverage Viewer module• (See next slide for instructions)

• Write a Unit Test• Run test and view highlighted

code

Page 12: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Installing the Code Coverage Plugin Go into Netbeans -> Tools -> Plugins

Search for "Code Coverage" and install it.

If you don't see it in the list of available plugins, go to the "Settings" tab and check "Netbeans Beta”, reload the catalog, and then try again.

If you don't see Netbeans Beta in the list on Settings, add the following source for plugins:

Netbeans Beta

http://updates.netbeans.org/netbeans/updates/6.8/uc/final/beta/catalog.xml.gz

Page 13: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

How do you you combine coverage with traditional system test scripts? Traditional system testing uses scripts- 1. Enter salary- 2. Enter number of dependents- 3. Click “Calculate Taxes” button- …

To combine this with coverage, launch the GUI while capturing coverage statistics

Run the test case to determine coverage

Page 14: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

How to write test cases

See sample system test case http://www.cs.gmu.edu/~kdobolyi/cs421/

SAMPLESystemTestCase.doc

See sample unit test case http://www.cs.gmu.edu/~kdobolyi/cs421/

Main.java http://www.cs.gmu.edu/~kdobolyi/cs421/

Triangle.java http://www.cs.gmu.edu/~kdobolyi/cs421/

Triangle.java

Page 15: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Class Exercise - Lets try it out! Using the

SystemRequirementsSpecificationExample.doc

Each team pick a use caseDocument the overall system

or unit tests you would write to test

Do the same for a non-functional requirement (sec 5)

Page 16: Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Summary

Unit tests can help test the details of your program

Automated unit tests provide constant visibility and easy retesting

Test coverage supplies valuable information when running both unit tests and system tests