junit don braffitt updated: 10-jun-2011

28
JUnit Don Braffitt http://www.radford.edu/dbraffitt/ Updated: 10-Jun-2011

Upload: rudolph-joseph

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnitDon Braffitthttp://www.radford.edu/dbraffitt/Updated: 10-Jun-2011

Page 2: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Pocket Guide

• JUnit Pocket Guide• By: Kent Beck• Publisher: O'Reilly Media, Inc.• Pub. Date: September 23, 2004• Print ISBN-13: 978-0-596-00743-0• Pages in Print Edition: 90

Page 3: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Overview

• Created by Kent Beck and Erich Gamma• Java open source framework for test-driven

development• Appeals to programmers using an agile (iterative

and incremental) programming method such as Extreme Programming which advocates frequent releases in short development cycles

Page 4: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Automating Tests

• Consider this starting point

• Now consider this initial testing effort

List fixture= new ArrayList( );// fixture should be emptyObject element= new Object( );fixture. add(element);// fixture should have one element

List fixture= new ArrayList( );System.out.println(fixture.size( ));Object element= new Object( );fixture. add(element);System.out.println(fixture.size( ));

Page 5: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Automating Tests

• Now do output only if a failure is detected

• Now build a new method to handle the output

List fixture= new ArrayList( );System.out.println(fixture.size( ) == 0);Object element= new Object( );fixture. add(element);System.out.println(fixture.size( ) == 1);

void assertTrue(boolean condition) throws Exception { if (! condition) throw new Exception("Assertion failed");}

Page 6: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Automating Tests

• Finally, use the new AssertTrue method

List fixture= new ArrayList( );assertTrue(fixture.size( ) == 0);Object element= new Object( );fixture. add(element);assertTrue(fixture.size( ) == 1);

Page 7: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Why Test?

• JUnit does the following:• Runs tests automatically• Runs many tests together and summarizes the results• Provides a convenient place to collect the tests you’ve

written• Provides a convenient place for sharing the code used

to create the objects you are going to test• Compares actual results to expected results and

reports differences

Page 8: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Why Test?

• Win-win-win programming practice• A win for me in the short term• A win for me in the long term• A win for my teammates and customers

Page 9: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Why Test?

• Defect Cost Increase (DCI)• The sooner you test after the creation of an error, the

greater your chance of finding the error and the less it costs to find and fix the error

Page 10: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Goals

• Simultaneously tests should be:• Easy to write. Test code should contain no extraneous

overhead.• Easy to learn to write. Because our target audience for JUnit is

programmers who are not usually professional testers, we would like to minimize the barriers to test writing.

• Quick to execute. Tests should run fast so we can run them hundreds or thousands of times a day.

• Easy to execute. The tests should run at the touch of a button and present their results in a clear and unambiguous format.

• Isolated. Tests should not affect each other. If the order in which the tests are run changes, the results shouldn’t change.

• Composable. We should be able to run any number or combination of tests together. This is a corollary of isolation.

Page 11: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit – Goals

• There are two main clashes between these constraints:• Easy to write versus easy to learn to write. Tests do

not generally require all the flexibility of a programming language, especially not an object language.• Isolated versus quick to execute. If you want the

results of one test to have no effect on the results of another test, each test should create the full state of the world before it begins to execute and return the world to its original state when it finishes.

Page 12: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Fixtures• setUp()• tearDown()• Move variable parts of setUp() to test methods• No convenient support for suite-level setup

Page 13: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Testing Exceptions

Page 14: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit Implementation

• Consider a test case class with two test methods

public class EmptyTest extends TestCase { List empty= new ArrayList( ); public void testSize( ) { assertEquals(0, empty.size( )); } public void testIsEmpty( ) { assertTrue(empty.isEmpty( )); }}

Page 15: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit Implementation

• JUnit converts the test class into a TestSuite

Page 16: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit Implementation

• When the TestSuite is run, it runs each of the EmptyTest in turn• Each runs its own setUp( ) method, creating a

fresh ArrayList for each test

Page 17: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit Implementation

• To run the test method itself, JUnit uses reflection to find the method named in fName and invokes it• This trick is called Pluggable Selector in the

Smalltalk world• You can’t look at the code to decide whether a

function is invoked, you have to look at the data values at runtime.• Pluggable Selectors in JUnit make writing the

tests much simpler

Page 18: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit Implementation

• Summary• One test-case class results in a two-level tree of

objects when the tests are run• Each test method works with its own copy of the

objects created by setUp( )• The result is tests that can run completely

independently

Page 19: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit’s API

• Five main classes or interfaces• Assert - A collection of static methods for checking

actual values against expected values• Test - The interface of all objects that act like tests• TestCase - A single test• TestSuite - A collection of tests• TestResult - A summary of the results of running

one or more tests

Page 20: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Test-First Programming

• Part of test-driven development• White-box tests for whole specification• Incremental tests as you gradually add specified

functionality• Tests are expected to fail since you don’t have

complete functionality to test

Page 21: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Stubs

• Stubs don’t faithfully reproduce the behavior of the intended object• Always better to have at least one test case

failing while the stub is in place

Page 22: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Other Uses for Tests

• Debugging Tests• Learning an API with Tests• Documenting Assumptions with Tests• Cross-Team Tests

Page 23: JUnit Don Braffitt  Updated: 10-Jun-2011

Story of JUnit

• Kent Beck 1994• Smalltalk testing framework (3 classes 12

methods)• Eric Gamma 1997• With Kent Beck, built most of JUnit on a plane in 3

hours

Page 24: JUnit Don Braffitt  Updated: 10-Jun-2011

Extending JUnit

• Add your own extensions• Subclass TestCase• Write new Assert classes• Write new Test Runner

• Pre-built extensions• JUnitPerf• HttpUnit• JWebUnit and many others

Page 25: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit and Ant

Page 26: JUnit Don Braffitt  Updated: 10-Jun-2011

Running JUnit Standalone

Page 27: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit and IDEs

Page 28: JUnit Don Braffitt  Updated: 10-Jun-2011

JUnit - Test Infection

• Write lots of tests• Runs your tests often• Learn the design skills necessary to write tests

that are simple and run fast• Start sharing your skills with others