1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,

Slides:



Advertisements
Similar presentations
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.
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.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
JUnit intro Kalvis Apsitis. What are “Programmer Tests”? Programmer Testing is the testing performed by a developer with the goal of verifying the correct.
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.
Integration Testing When testing a module in isolation –Called modules need to be replaced –Tested module needs to be called A D ′ E ′ main driver module.
CMSC 345, Version 11/07 SD Vick from S. Mitchell Software Testing.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
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.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
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.
Software Testing Prof. Dr. Holger Schlingloff Humboldt-Universität zu Berlin and Fraunhofer Institute of Computer Architecture and Software Technology.
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.
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:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
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
Testing Especially Unit Testing. V-model Wikipedia:
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
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.
Test automation / JUnit Building automatically repeatable test suites.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
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.
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
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.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
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 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.
2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.
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.
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
Chapter 8 – Software Testing
Verification and Testing
Computer Science 209 Testing With JUnit.
JUnit Automated Software Testing Framework
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Advanced programming Practices
Section 4: Graphs and Testing
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.
Presentation transcript:

1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available from:

2 Unit Testing  Introduction  Conventional approach  Unit testing with JUnit  More on JUnit

3 Introduction  Testing A process of showing that a program works for certain inputs  Phases Unit testing  To test each module (unit, or component) independently  Mostly done by developers of the modules Integration and system testing  To test the system as a whole  Often done by separate testing or QA team Acceptance testing  To validate system functions for (and by) customers or user

4 What Is Unit Testing?  Testing focusing on the smallest units of code, such as Functions, procedures, subroutines, subprograms Methods, classes  Component tested in isolation from the rest of the system and in a controlled environment: Uses appropriately chosen input data Uses component-level design description as guide  Unit testing is testing of a unit.  Often the target of testing frameworks such as JUnit

5 Unit in Java?  A unit is a module or a small set of modules.  In Java, a unit is a class or interface, or a set of them, e.g., An interface and 3 classes that implement it A public class along with its helper classes.

6 Question  Do you get more confidence by running more test data?

7 Why Unit Testing?  Code isn’t right if it’s not tested.  Practical Most programmers rely on testing, e.g., Microsoft has 1 tester per developer. You could get work as a tester.  Divide-and-conquer approach Split system into units. Debug unit individually. Narrow down places where bugs can be. Don’t want to chase down bugs in other units.

8 Why Unit Testing? (Cont.)  Support regression testing So can make changes to lots of code and know if you broke something. Can make big changes with confidence.

9 How to Do Unit Testing  Build systems in layers Starts with classes that don’t depend on others. Continue testing building on already tested classes.  Benefits Avoid having to write (test) stubs. When testing a module, ones it depends on are reliable.

10 Question  How does low coupling help testing?  How does high coupling hurt it?

11 Program to Test public class IMath { /** Returns an integer approximation to the square root of x. */ public static int isqrt(int x) { int guess = 1; while (guess * guess < x) { guess++; } return guess; }

12 Conventional Testing /** A class to test the class IMath. */ public class IMathTestNoJUnit { /** Runs the tests. */ public static void main(String[] args) { printTestResult(0); printTestResult(1); printTestResult(2); printTestResult(3); printTestResult(4); printTestResult(7); printTestResult(9); printTestResult(100); } private static void printTestResult(int arg) { System.out.print(“isqrt(“ + arg + “) ==> “); System.out.println(IMath.isqrt(arg)); }

13 Conventional Test Output Isqrt(0) ==> 1 Isqrt(1) ==> 1 Isqrt(2) ==> 2 Isqrt(3) ==> 2 Isqrt(4) ==> 2 Isqrt(7) ==> 3 Isqrt(9) ==> 3 Isqrt(100) ==> 10  What does this say about the code? Is it right?  What’s the problem with this kind of test output?

14 Solution?  Automatic verification by testing program Can write such a test program by yourself, or Use a testing tool such as JUnit  JUnit A simple, flexible, easy-to-use, open-source, and practical unit testing framework for Java Can deal with a large and extensive set of test cases Refer to

15 Testing with JUnit import org.junit.Test; import static org.junit.Assert.*; /** A JUnit test class to test the class IMath. */ public class IMathTest { /** Test isqrt. public void testIsqrt() { assertEquals(0, IMath.isqrt(0)); // line 13 assertEquals(1, IMath.isqrt(1)); assertEquals(1, IMath.isqrt(2)); assertEquals(1, IMath.isqrt(3)); assertEquals(2, IMath.isqrt(4)); assertEquals(2, IMath.isqrt(7)); assertEquals(3, IMath.isqrt(9)); assertEquals(10, IMath.isqrt(100)); }

16 Testing with JUnit (Cont.) /** Run the tests. */ public static void main(String[] args) { org.junit.runner.JUnitCore.main("IMathTest"); }

17 Compilation and Output $ javac IMath.java IMathTest.java $ java IMathTest JUnit version 4.10.E Time: There was 1 failure: 1) testIsqrt(IMathTest) java.lang.AssertionError: expected: but was: at org.junit.Assert.fail(Assert.java:93) … at IMathTest.main(IMathTest.java:13) FAILURES!!! Tests run: 1, Failures: 1 Question: Is this better? If so, why?

18 Exercise  Write a JUnit test class for testing public class ForYou { /** Return the minimum of x and y. */ public static int min(int x, int y) {... } }

19 Exercise (Cont.) public class ForYouTest { /** Test min. public void testMin() { } // the rest as before … } public class ForYou { /** Return the minimum of x and y. */ public static int min(int x, int y) {... } } In Eclipse, - Select a source code folder - Right click (pop-up menu) - Select New > Other… > JUnit > Junit Test Case

20 Some Terminology  Definition A test data (or case) for a method M is a pair of (o, args), where  o is not null and M can be sent to o,  args is a tuple of arguments that can be passed to M. A test data, (o, args), for M succeeds iff o.M(args) behaves as expected. A test data, (o, args), for M fails iff it does not behave as expected.  Question Why should o not be null? If M has a bug that is revealed by a test data, does that test data for M succeeds or fails?

21 Parts of Test Code  Definition The test fixture is the set of variables used in testing. The test driver is the class that runs the tests. The test oracle for a test data is the code that decides success or failure for that test data.  Question What in the code we saw so far was the test driver, and the oracle? What difference is there between JUnit testing and non-JUnit testing in what we saw before?

22 Basic Usage of JUnit To test a type T: 1. Write a class like: import org.junit.*; import static org.junit.Assert.*; /** A JUnit test class for the class T. */ public class TTest { /** Runs the tests. */ public static void main(String[] args) { org.junit.runner.JUnitCore.main(“T") public void testM1() { … } }

23 Basic Usage of JUnit (Cont.) 2. Compile T.java and TTest.java, e.g., $ javac T.java TTest.java 3. Run the JUnit in IDE such as Eclipse, e.g., Select Run As > JUnit Test or Run the text interface $ java TTest 4. Look at the failures and errors

24 Naming Convention  Test methods start with “test” e.g., testIsqrt, testMin  Test classes end with “Test” e.g., IMathTest, ForYouTest

25 Assertion Methods Method Description assertEquals(a,b)Test if a is equal to b assertFalse(a)Test if a is false assertNotSame(a, b)Test if a and b do not refer to the identical object assertNull(a)Test if a is null assertSame(a,b)Test if a and b refer to the identical object assertTrue(a)Test if a is true fail()Fail a test - Static methods defined in org.junit.Assert - Variations taking string error messages

26 Testing Exceptions  Use optional parameter “expected” annotation  Q: Without using public void indexingEmptyList() { new ArrayList ().get(0); }

27 More on JUnit -- Test Fixture  Sharing test data among test methods public class TTest { // test fixture variables, i.e., fields shared by several test public void setUp() throws Exception { // initialize test fixture variables. public void tearDown() throws Exception { // uninitialize test fixture variables. } // test methods … } Also refer annotations.

28 Example public class PointTest { private Point public void setUp() { p = new Point(10, 10); public void testSetX() { p.setX(20); assertEquals(20, p.getX()); assertEquals(10, p.getY()); public void testSetY() { p.setY(30); assertEquals(30, p.getY()); assertEquals(10, p.getX()); } Q: What is omitted and p is initialized to new Point(10,10)?

29 More on JUnit -- Test Suite  Definition A test suite is a set of test methods and other test suites.  Test Suite Organize tests into a larger test set. Help with automation of testing.

30 Example import org.junit.runner.RunWith; PlaceTest.class, BattleshipTest.class, BattleFieldTest.class }) public class AllTests { } In Eclipse, - Select a source code folder - Right click (pop-up menu) - Select New > Other … > JUnit > Junit Test Suite

31 More on JUnit?  Refer to