Unit Testing. Topics Motivation JUnit framework Basic JUnit tests – static methods Ensure exceptions – instance methods.

Slides:



Advertisements
Similar presentations
Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? © 2007 Christian Hujer.
Advertisements

J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
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.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Road Map Introduction to object oriented programming. Classes
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Java Development Tools project.
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.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
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 test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
(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
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Dwight Deugo Nesa Matic
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
JUnit SWE 619 Summer July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable.
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,
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.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Java IDE Dwight Deugo Nesa Matic
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Unit Testing with JUnit
This presentation is created for the course COP4331 at UCF
CompSci 230 Software Construction
Junit with.
JUnit Automated Software Testing Framework
JUnit 28-Nov-18.
Test-Driven Development
Introduction to JUnit IT323 – Software Engineering II
Testing Acknowledgement: Original slides by Jory Denny.
Constructors, GUI’s(Using Swing) and ActionListner
CS 240 – Advanced Programming Concepts
CSE 143 Lecture 5 More ArrayIntList:
JUnit Reading: various web pages
Test-Driven Development
Joel Adams and Jeremy Frens Calvin College
JUnit SWE 619 Spring 2008.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Methods/Functions.
Presentation transcript:

Unit Testing

Topics Motivation JUnit framework Basic JUnit tests – static methods Ensure exceptions – instance methods Test floating point values

JUnit Motivation Goal: relieve human from task of comparing expected and actual values public class Utilities { public static int calcPerimeter(int length, int width) { return 2 * (length + width); } public static void main(String [] args) { // Ensure output is 16 System.out.println(Utilities.calcPerimeter(2,6)); }

Test-Driven Development (TDD) 1.Add a test (or tests) for an new unit of functionality (unit === method) 2.Run prior tests, see new test fail –may write stubs so code compiles 3.Implement new functionality 4.Run tests, see them succeed 5.Refactor – clean code rocks! 6.Repeat

Tests First Some “easy” examples –How would you test a function to calculate the area of a triangle? –How would you test a function to calculate the circumference of a circle? –How would you test a function that calculates wind chill?

JUnit Framework Test Case. Method that runs one or more tests. Test Suite. Collection of tests run at the same time. Includes one or more test cases/methods. Test Runner. Utility used to run a test suite. –In Eclipse, shows a graphical presentation. –May also be a command-line version (Ruby). JUnit framework available from

Packages in Java A package is a way to bundle classes by functionality Customers don’t really want to see all your test code!

Create a Package In Eclipse: Create a Java project, click on src Select new package icon Enter an appropriate name For this example, create a package named windchill* Select the package, create a new class named MyTempConverter * for distribution, you’d want to ensure unique

More on Packages The package statement must be the first line in the file, e.g.: package windchill; public class MyTempConverter { … } Each package has its own directory (in the file structure)

TDD Example: Windchill See the formula and chart: Windchill formula: T V (**0.16) TV(**0.16)

The TempConverter class First we create the “failing” methods – so that our tests will at least compile package windchill; public class MyTempConverter { public static long windChill(int speed, int temperature) { return -1; } Why does it make sense for this method to be static?

Adding the tests Create a package for your tests Add your test classes to the package –Select the test package –Create a new class of type JUnit Test Case

Eclipse and JUnit 1. Select JUnit Test Case 2. Select JUnit Test Case We’ll use JUnit 4 Pick a meaningful name click

Eclipse and JUnit continued Dialog for class to test: You must type in package Then you’ll see possible classes

Eclipse prompts for library

Build Path If Eclipse creates a JUnit test for you, you’ll be prompted to add library. To add a library later: Right-click on project name (in package explorer) Select Build Path Select Add Libraries Select JUnit 4

JUnit v4 New Java Feature: annotations Places a “marker” in code that is interpreted by another tool For JUnit, marker/annotation No need to extend TestCase (JUnit3) Must import org.junit.*;

Some windchill tests package tests; import junit.framework.Assert; import static org.junit.Assert.*; import Windchill.MyTempConverter; public class TestWindchillCalcs public void testWindchill() { long expected = -11; long actual = MyTempConverter.windChill(5, 0); assertEquals(expected, actual); // local variables not needed assertEquals(3, MyTempConverter.windChill(10, 15));} }

The TempConverter class Now add code so that the tests pass package Windchill; public class MyTempConverter { public static long windChill(int speed, int temperature) { double newTemp = *temperature * Math.pow(speed, 0.16) * temperature * Math.pow(speed,0.16); return Math.round(newTemp); } public static void main(String[] args) { System.out.println(MyTempConverter.windChill(10, 30)); }

Run as JUnit Test

JUnit Execution All is well! Error! Look at failure trace for explanation

Can also test for expected exceptions package Windchill; public class BadInputException extends RuntimeException { public BadInputException() {} public BadInputException(String msg){ super(msg); (expected = BadInputException.class) public void testWindchillLowSpeed() throws BadInputException { long actual = MyTempConverter.windChill(4, 10); } public static long windChill(int speed, int temperature) { if (speed < 5) throw new BadInputException("Windchill not valid if speed < 5"); double newTemp = *temperature * Math.pow(speed, 0.16) * temperature * Math.pow(speed,0.16); return Math.round(newTemp); } Exception class JUnit test

Discuss with a partner When would we need the throws clause on the test? What happens if the (expected) phrase is removed?

Testing instance methods When testing a class that has instance methods (most often), the test will need to create an annotated methods will be run exactly once during your test run - at the very beginning and end of the test as a whole, before anything else is run. In fact, they're run before the test class is even constructed, which is why they must be declared static. methods will be run before and after every test case, so will probably be run multiple times during a test run

Example to package game; public class Location { private int x, y; public void move(int dx, int dy) { x += dx; y += dy; } // Also has constructors and getters }

Test it – message public class TestLocation { private Location public void setUp(){ location = new Location(); public void testMove() { location.move(5, 10); assertEquals(5, location.getX()); assertEquals(10, location.getY()); public void testMove2() { location.move(5, 10); assertEquals(5, location.getX()); assertEquals(10, location.getY()); location.move(5, 10); assertEquals(10, location.getX()); assertEquals(20, location.getY()); }

Example Assume the board set up is complex, and you don’t need to reset between tests. package game; public class Board { private String gameStatus; public void init() { gameStatus = "Long game set up is done!"; } public String getGameStatus() { return gameStatus; }

test import game.Board; public class TestBoard { static Board public static void setUpBeforeClass() throws Exception { board = new Board(); board.init(); public void test1() { assertEquals(board.getGameStatus(), "Long game set up is done!"); public void test2() { assertEquals(board.getGameStatus(), "Long game set up is done!"); not run for each test instance – just the entire test suite – so variables must be static This is an advanced detail – will not be covered on tests, BUT may be helpful in your projects

Testing floating points Remember that floating point values should not be compared exactly. public class MyConverter { public static double INCHES_TO_METERS = ; public static double englishToMeters(int feet, int inches) { return 0; //int totalInches = feet * 12 + inches; //return totalInches * INCHES_TO_METERS; }

The test Use a tolerance public class TestConversions { public static double EPSILON public void testFeetToMeters() { double expected = ; double actual = MyConverter.englishToMeters(0, 2); assertEquals(expected, actual, EPSILON); expected = ; actual = MyConverter.englishToMeters(1, 2); assertEquals(expected, actual, EPSILON); }

What tests could you write for… A chess game A linked list library String library UPC code

Advanced Topic The static import construct allows unqualified access to static members import static public void testMove3() { location.move(5, 10); assertEquals(5, location.getX()); assertEquals(10, location.getY()); }