Unit testing. JUnit. JUnit and Eclipse. A JUnit test class 3/6/17. A method is flagged as a JUnit test case.

Similar documents
Unit testing. unit testing: Looking for errors in a subsystem in isolation. The basic idea: JUnit provides "assert" commands to help us write tests.

CSE 331. Unit Testing with JUnit

UNIT TESTING. Krysta Yousoufian CSE 331 Section April 19, With material from Marty Stepp, David Notkin, and The Pragmatic Programmer

Section 4: Graphs and Testing

Section 4: Graphs and Testing

Section 4: Graphs and Testing. Slides by Erin Peach and Nick Carney

CSE332: Data Abstrac0ons Sec%on 4. HyeIn Kim CSE 331 Slides Spring 2013

CSE332: Data Abstrac0ons Sec%on 3

CSE 403 Lecture 13. Black/White-Box Testing. Reading: Software Testing: Principles and Practices, Ch. 3-4 (Desikan, Ramesh)

Introduction to JUnit

CSE332: Data Abstrac0ons Sec%on 3. HyeIn Kim CSE 331 Slides (JUnit/Javadoc) Spring2014

Array Based Lists. Collections

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

CS159. Nathan Sprague. September 30, 2015

Building Java Programs

CS18000: Programming I

JUnit Framework. Terminology: assertions, annotations, fixtures. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Software-Architecture Unit Testing (JUnit)

JUnit Testing Framework Architecture

CSE 331. Programming by contract: pre/post conditions; Javadoc

Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (testing whatever occurs to you at

public static void negate2(list<integer> t)

public static boolean isoutside(int min, int max, int value)

linaye/gl.html

Testing and Debugging

Building Java Programs

Agile Software Development. Lecture 7: Software Testing

Test-Driven Development JUnit

Test-Driven Development JUnit

Unit Testing. SWEN-261 Introduction to Software Engineering. Department of Software Engineering Rochester Institute of Technology

CMSC 132, Object-Oriented Programming II Summer Lecture 5:

Java Review via Test Driven Development

Agenda. JUnit JaCoCo Project. CSE 4321, Jeff Lei, UTA

Introduc)on to tes)ng with JUnit. Workshop 3

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

Test automation / JUnit. Building automatically repeatable test suites

Test automation Test automation / JUnit

Object Oriented Software Design - I

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Ex 1. public float temperature(float powsupply, float clock, char cooler)

CSC-140 Assignment 6

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

Testing and Debugging

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

Programming Project: Game of Life

Class design guidelines. Most of this material comes from Horstmann, Cay: Object-Oriented Design & Patterns (chapter 3)

EECS 4313 Software Engineering Testing

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

CSCE 747 Unit Testing Laboratory Name(s):

Tools for Unit Test - JUnit

Test automation / JUnit. Building automatically repeatable test suites

Chapter 3. Unit Testing with JUnit and Debugging. Testing with JUnit getting started. Note

JUnit + JMock Tomáš Soukup

Testing. Technion Institute of Technology Author: Assaf Israel. Author: Assaf Israel - Technion

Testing. CMSC 433 Programming Language Technologies and Paradigms Spring A Real Testing Example. Example (Black Box)?

Tools for Unit Test JUnit

CMSC131. Library Classes

COMP 354 TDD and Refactoring

Checking Current Code Coverage

Thinking Functionally

Assertions, pre/postconditions

Motivating Example: Two Types of Errors (2) Test-Driven Development (TDD) with JUnit. Motivating Example: Two Types of Errors (1)

Test-Driven Development (TDD) with JUnit

Advanced if/else & Cumulative Sum

JUnit in EDA Introduction. 2 JUnit 4.3

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Classes, objects. Static, const, mutable members of a class

Unit Testing Activity

Software Development Tools. COMP220/COMP285 Sebastian Coope Eclipse and JUnit: Creating and running a JUnit test case

Programming Project: ArrayFun

Binghamton University. CS-140 Fall Unit Testing

Test-Driven Development (a.k.a. Design to Test) CSE260, Computer Science B: Honors Stony Brook University

Provided Files that Are Fine

Charlie Garrod Michael Hilton

Memory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CSE 143 Lecture 4. Preconditions

Topics covered. Introduction to JUnit JUnit: Hands-on session Introduction to Mockito Mockito: Hands-on session. JUnit & Mockito 2

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

Simple TDD Case Study. in Java

CSE 143 Lecture 2. reading:

Binghamton University. CS-140 Fall Unit Testing

print statements, debugger expressions, test scripts. Writing expressions in a debugger only that t a program works now. An application typically

Unit Testing : Software Testing

Junit Overview. By Ana I. Duncan

Introduction to JUnit. Data Structures and Algorithms for Language Processing

COE318 Lecture Notes Week 9 (Oct 31, 2011)

xtreme Programming (summary of Kent Beck s XP book) Stefan Resmerita, WS2015

Advanced Software Engineering

Program Correctness and Efficiency. Chapter 2

Implement factorial to return n! that is defined as n * n-1 * n-2 * n-3 * 2 * 1. 1! And 0! Are both defined as 1. Use recursion, no loop.

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name:

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Programming 2. Object Oriented Programming. Daniel POP

Good Luck! CSC207, Fall 2012: Quiz 3 Duration 25 minutes Aids allowed: none. Student Number: Lecture Section: L0101. Instructor: Horton

An introduction to formal specifications and JML. Invariant properties

Software Engineering I (02161)

White-box testing. Software Reliability and Testing - Barbara Russo SERG - Laboratory of Empirical Software Engineering.

Transcription:

Unit testing JUnit ENGI 5895 unit testing: Looking for errors in a subsystem in isolation. Generally a "subsystem" means a particular class or object. The Java library JUnit helps us to easily perform unit testing. The basic idea: For a given class Foo, create another class FooTest to test it, containing various "test case" methods to run. Each method looks for particular results and passes / fails. Adapted from slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/ JUnit provides "assert" commands to help us write tests. The idea: Put assertion calls in your test methods to check things you expect to be true. If they aren't, the test will fail. 1 2 JUnit and Eclipse To add JUnit to an Eclipse project, click: Project Properties Build Path Libraries Add Library JUnit JUnit 4 Finish To create a test case: right-click a file and choose New Test Case or click File New JUnit Test Case Eclipse can create stubs of method tests for you. A JUnit test class import org.junit.*; import static org.junit.assert.*; public class name { public void name() { // a test case method A method with is flagged as a JUnit test case. All methods run when JUnit runs your test class. 3 4 1

JUnit assertion methods ArrayIntList JUnit test asserttrue(test) assertfalse(test) assertequals(expected, actual) fails if the boolean test is false fails if the boolean test is true fails if the values are not equal assertsame(expected, actual) fails if the values are not the same (by ==) assertnotsame(expected, actual) fails if the values are the same (by ==) assertnull(value) assertnotnull(value) fail() fails if the given value is not null fails if the given value is null causes current test to immediately fail Each method can also be passed a string to display if it fails: e.g. assertequals("message", expected, actual) 5 import org.junit.*; import static org.junit.assert.*; public class TestArrayIntList { public void testaddget1() { ArrayIntList list.add(42); list.add(-3); list.add(15); assertequals(42, list.get(0)); assertequals(-3, list.get(1)); assertequals(15, list.get(2)); public void testisempty() { list = new ArrayIntList(); ArrayIntList asserttrue(list.isempty()); list.add(123); assertfalse(list.isempty()) ; list.remove(0); asserttrue(list.isempty()); list = new ArrayIntList(); 6 Running a test JUnit exercise Right click it in the Eclipse Package Explorer at left; choose: Run As JUnit Test The JUnit bar will show green if all tests pass, red if any fail. The Failure Trace shows which tests failed, if any, and why. Given a Date class with the following methods: public Date(int year, int month, int day) public Date() // today public int getday(), getmonth(), getyear() public void adddays(int days) // advances by days public int daysinmonth() public String dayofweek() // e.g. "Sunday" public boolean equals(object o) public boolean isleapyear() public void nextday() // advances by 1 day public String tostring() Test the adddays method 7 8 2

What's wrong with this? assertequals(d.getyear(), 2050); assertequals(d.getmonth(), 2); assertequals(d.getday(), 19); Well-structured assertions assertequals(2050, d.getyear()); assertequals(2, d.getmonth()); assertequals(19, d.getday()); // expected // value should // be at LEFT assertequals(d.getyear(), 2050); assertequals(d.getmonth(), 3); assertequals(d.getday(), 1); assertequals("year after +14 days", 2050, d.getyear()); assertequals("month after +14 days", 3, d.getmonth()); assertequals("day after +14 days", 1, d.getday()); // test cases should usually have messages explaining // what is being checked, for better failure output 9 10 Expected answer objects Date expected = new Date(2050, 2, 19); assertequals(expected, d); // use an expected answer // object to minimize tests // (Date must have tostring // and equals methods) Date expected = new Date(2050, 3, 1); assertequals("date after +14 days", expected, d); Naming test cases public void test_adddays_withinsamemonth() { Date actual = new Date(2050, 2, 15); actual.adddays(4); Date expected = new Date(2050, 2, 19); assertequals("date after +4 days", expected, actual); // give test case methods really long descriptive names public void test_adddays_wraptonextmonth() { Date actual = new Date(2050, 2, 15); actual.adddays(14); Date expected = new Date(2050, 3, 1); assertequals("date after +14 days", expected, actual); // give descriptive names to expected/actual values 11 12 3

Tests with a timeout (timeout = 5000) public void name() { The above method will be considered a failure if it doesn't finish running within 5000 ms private static final int TIMEOUT = 2000; (timeout = TIMEOUT) public void name() { Times out / fails after 2000 ms Testing for exceptions (expected = ExceptionType.class) public void name() { Will pass if it does throw the given exception. If the exception is not thrown, the test fails. Use this to test for expected errors. (expected = ArrayIndexOutOfBoundsException.c lass) public void testbadindex() { ArrayIntList list = new ArrayIntList(); list.get(4); // should fail 13 14 Setup and teardown @Before public void name() { @After public void name() { methods to run before/after each test case method is called @BeforeClass public static void name() { @AfterClass public static void name() { methods to run once before/after the entire test class runs Tips for testing You cannot test every possible input, parameter value, etc. So you must think of a limited set of tests likely to expose bugs. Think about boundary cases positive; zero; negative numbers right at the edge of an array or collection's size Think about empty cases and error cases 0, -1, null; an empty list or array test behavior in combination maybe add usually works, but fails after you call remove make multiple calls; maybe size fails the second time only 15 16 4

JUnit summary Tests need failure atomicity (ability to know exactly what failed). Each test should have a clear, long, descriptive name. Assertions should always have clear messages to know what failed. Write many small tests, not one big test. Each test should have roughly just 1 assertion at its end. Test for expected errors / exceptions. Choose representative test cases from equivalent input classes. Avoid complex logic in test methods if possible. Use helpers, @Before to reduce redundancy between tests. 17 5