JUnit Don Braffitt Updated: 10-Jun-2011.

Slides:



Advertisements
Similar presentations
Unit Testing Australian Development Centre Brisbane, Australia.
Advertisements

1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
J-Unit Framework.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Approach of Unit testing with the help of JUnit Satish Mishra
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
JUnit, Revisited 17-Apr-17.
22-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
26-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
1 CSC/ECE 517 Fall 2010 Lec. 2 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
The Design of JUnit Yonglei Tao. Test-First Development  An essential element in eXtreme Programming (XP)  Test is written before the code  As an executable.
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Chapter 3 – Agile Software Development 1Chapter 3 Agile software development.
Introduction to Unit Testing Jun-Ru Chang 2012/05/03.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
T-unit: Tcl Unit Test Package Automated Unit Test Package For Tcl Procedures Final Presentation Joseph Boyle Loyola Marymount University.
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
JUnit & Eclipse1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Adam Heath. What is JUnit?  JUnit is a unit testing framework for the Java programming language  It allows developers to swiftly and easily test.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
JUnit, Bugzilla James Atlas July 24, 2008 *part of today’s slides courtesy of Dwight Deugo and Nesa Matic under the EPL.
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
PHPUnit Julia Bondar IAPM23. Agenda What is PHPUnit How to write an automated test Writing and running tests with PHPUnit Advantages and disadvantages.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Chapter 3 Agile software development 1 Chapter 3 – Agile Software Development.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
History, Characteristics and Frameworks
Selenium HP Web Test Tool Training
Introduction to JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Joel Adams and Jeremy Frens Calvin College
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Presentation transcript:

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: Pages in Print Edition: 90

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

JUnit - Automating Tests Consider this starting point Now consider this initial testing effort List fixture= new ArrayList( ); // fixture should be empty Object 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( ));

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"); }

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);

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

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

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

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.

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.

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

JUnit - Testing Exceptions

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( )); }

JUnit Implementation JUnit converts the test class into a TestSuite

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

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

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

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

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

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

JUnit - Other Uses for Tests Debugging Tests Learning an API with Tests Documenting Assumptions with Tests Cross-Team Tests

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

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

JUnit and Ant

Running JUnit Standalone

JUnit and IDEs

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