Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.

Slides:



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

Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
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.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Java Development Tools project.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
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.
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.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
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.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
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.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
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.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Introduction to Unit Testing Jun-Ru Chang 2012/05/03.
Principles of Object Oriented Programming Practical session 2 – part A.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Coverage tools Program is typically compiled with special options, to add extra source or object code. –Additional data structures, such as a flow graph,
Testing in Extreme Programming
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
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.
Test automation / JUnit Building automatically repeatable test suites.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
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.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
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.
Unit Testing with FlexUnit
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.
Test automation / JUnit Building automatically repeatable test suites.
Automated Testing with PHPUnit. How do you know your code works?
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
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 Java programs Using JUnit
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
Unit testing C# classes
Overview of Eclipse Lectures
Test-first development
Test-driven development (TDD)
Chapter 11, Testing.
Test-Driven Development
Introduction to JUnit IT323 – Software Engineering II
JUnit Reading: various web pages
Test-Driven Development
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:

Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site

Unit testing with Junit and Clover2 JUnit ( A unit test framework for Java Authors: Erich Gamma, Kent Beck Part of XUnit family (HTTPUnit, Cactus), CppUnit Essential part of the eXtreme Programming methodology, but can be used independently Used for regression testing as well, but not for system testing Integrated to Eclipse, but can be used standalone

Unit testing with Junit and Clover3 eXtreme Programming (XP) and unit testing In XP, a test shall: Be written first —before any code Executed —will likely fail! Then: Implementation code should be written that would be the minimum code required to get the test to pass – and no extra functionality. Once the code is written, re-execute the test and it should pass. When needed, refactor the code mercilessly. —Improve performance, maintainability, readability

Unit testing with Junit and Clover4 Common XP day

Unit testing with Junit and Clover5 Some benefits of JUnit (and Test-Driven Development) Testing is a Good Thing Immediate gratification with build iterations Start with “The Simplest Thing That Could Possibly Work”. Green bar addiction! Break the cycle of “more pressure means fewer tests” Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Martin Fowler Reduce code captivity If others can test it, others can work on it.

Unit testing with Junit and Clover6 What is a JUnit test? A test “script” is just a collection of small Java methods. General idea is to create a few Java objects, do something interesting with them, and then determine if the objects have the correct properties. What is added? Assertions! A package of methods that checks various properties: — equality of variables — identity of objects The assertions are used to determine the test case verdict.

Unit testing with Junit and Clover7 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); }

Unit testing with Junit and Clover8 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); } Method signature: no parameters

Unit testing with Junit and Clover9 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); } Objective: create a duplicate object, instead of copying reference

Unit testing with Junit and Clover10 A JUnit test case /** Test of copy method, class ProblemBase.Value */ public void testCopy() { System.out.println("testCopy"); Value v1 = new Value( ); v1.setName( "X" ); Value v2 = v1.copy( ); v1.setName( "Y" ); String expected = "X"; String actual = v2.getName( ); Assert.assertEquals( expected, actual ); } Check for a condition that should not be violated

Unit testing with Junit and Clover11 A JUnit test class import junit.framework.*; // Each test class must extend the Junit // TestCase class public class CopyTest extends TestCase { // Must provide a constructor with String // argument public CopyTest(String name) { super(name); } // Insert your test cases here. setup(), // tearDown() and main() methods can also be // added. }

Unit testing with Junit and Clover12 Assertions and verdicts Assertions are defined in the special JUnit class Assert If the assertions are true, the method continues executing. If any assertion is false, the method stops executing, and the result for the test case will be “fail”. If any other exception is thrown during the method, the result for the test case will be “error”. If no assertions were violated for the entire method, the test case will “pass”. The Assert class name is often not required assertEquals( expected, actual );

Unit testing with Junit and Clover13 Assertion methods Assertion methods can verify: Objects are identical, or not identical Objects are null or non-null “Equality” of objects — via == or equals() depending on type Boolean conditions are true or false There is also an unconditional failure method.

Unit testing with Junit and Clover14 JUnit execution (with failures)

Unit testing with Junit and Clover15 JUnit execution (success!)

Unit testing with Junit and Clover16 JUnit plugin for Eclipse

Unit testing with Junit and Clover17 run(TestResult) addTest() TestSuite run(TestResult) Test *suite(): TestSuite AnotherTestClass run(TestResult) runTest() setup() tearDown() fName TestCase Your tests here. *suite(): TestSuite ATestClass fTests TestResult JUnit framework

Unit testing with Junit and Clover18 Some benefits of this framework No major difference between a test case and a test suite Both can be invoked using the run() method Test cases are often associated with methods, and test suites with classes Uses reflection to lean about classes and methods Test suite structure discovered at run time Test suites can invoke the test cases automatically Common setup and teardown (clean up) for all test cases in a test suite Default ones can be overriden Integrated to Uis, and wizard for test creations Simple, efficient, and automated!

Unit testing with Junit and Clover19 Java code coverage tool: Clover Discovers sections of code that are not being adequately exercised by your (unit) tests. Supports method, statement, and branch coverage Reports its findings in multiple formats —From project level down to individual lines of source code Historical charting of code coverage and other metrics Integrated to Eclipse and other IDEs

Unit testing with Junit and Clover20 Clover plugin for Eclipse

Unit testing with Junit and Clover21 Clover coverage filters One can choose not to instrument certain types of blocks in the code (e.g. assertions and exception catching), in order to focus on the coverage of interest.

Unit testing with Junit and Clover22 Clover plugin caveats This plugin may (very likely!) not work correctly if you have configured your Eclipse project so that the Java source and output directories are the same. —Use different directories for the original source code and instrumented source code When compiling your Java project with the Clover plugin, you must add and use a Java Development Kit (JDK) to your list of Installed JRE locations (see instructions). Not free software…

Unit testing with Junit and Clover23 Fixing the Clover plugin caveats

Unit testing with Junit and Clover24 For more information JUnit Web site Documentation on installation and use of JUnit E. Gamma, K. Beck, “JUnit Cookbook” Internals of JUnit “JUnit: A Cook’s Tour” Clover Eclipse Plugin (with installation instructions)