Tools for Unit Test JUnit

Similar documents
Tools for Unit Test - JUnit

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

Test-Driven Development JUnit

Test automation / JUnit. Building automatically repeatable test suites

Test automation Test automation / JUnit

Test-Driven Development JUnit

Test automation / JUnit. Building automatically repeatable test suites

EECS 4313 Software Engineering Testing

TEST DRIVEN DEVELOPMENT

Testing on Steriods EECS /30

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

Introduction to JUnit

Automated testing in Agile SW development

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

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

Verifying and Documenting ADTs: Javadoc, Java Assertions and JUnits

JUnit in EDA Introduction. 2 JUnit 4.3

Object Oriented Software Design - I

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

Announcements. Lab tomorrow. Quiz Thursday P3 due Friday. Exceptions and unit testing

Testing Stragegies. Black Box Testing. Test case

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

JUnit 3.8 Documented Using Collaborations

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

Testing with JUnit 1

linaye/gl.html

Problem other classes messing with my stuff!

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 3

4. A Testing Framework. Oscar Nierstrasz

Software Engineering I (02161)

Tuesday, November 15. Testing

4. A Testing Framework

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

COMP 354 TDD and Refactoring

Checking Current Code Coverage

Advanced Software Engineering

Software Testing Lecture 1. Justin Pearson

Java Review via Test Driven Development

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

South Africa

Functional Testing (Testování funkčnosti)

Junit Overview. By Ana I. Duncan

Software Test. Levels of test. Types of test. Regression test The JUnit tool for unit testing Java programs. System test Integration test Unit test

Unit Testing : Software Testing

Agile Software Development. Lecture 7: Software Testing

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

104. Intermediate Java Programming

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

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

CS 3 Introduction to Software Engineering. 3: Exceptions

Test-Driven Development (TDD) with JUnit

SWE 434 Software Testing and Validation

CSE 326: Data Structures. Section notes, 4/9/2009

More JUnit CS 4501 / 6501 Software Testing

Software Engineering I (02161)

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

Programmieren II. Unit Testing & Test-Driven Development. Alexander Fraser.

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

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

Simple TDD Case Study. in Java

CMSC131. Library Classes

Tutorial 3: Unit tests and JUnit

JUnit Testing Framework Architecture

Practical Objects: Test Driven Software Development using JUnit

Testing. Topics. Types of Testing. Types of Testing

Automating Regression Testing of Java Programs the JSnoopy Way

Testing and Debugging

Classes, interfaces, & documentation. Review of basic building blocks

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Procedural Abstraction

Unit Testing with JUnit and CppUnit

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

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

CS18000: Programming I

JUnit tes)ng. Elisa Turrini

Programming Embedded Systems

INTRODUCTION TO JAVA PROGRAMMING JAVA FUNDAMENTALS PART 2

Program Correctness and Efficiency. Chapter 2

Unit Testing with JUnit in DrJava *

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Unit Testing and JUnit

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

JUnit Testing Patterns: Mocking and Doubles

Chapter 15. Software Testing The assert Statement

JUNIT - API. The most important package in JUnit is junit.framework which contain all the core classes. Some of the important class are

CE221 Programming in C++ Part 1 Introduction

Tutorials for Struts, EJB, xdoclet and eclipse.

Software Engineering

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

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

Analysis of the Test Driven Development by Example

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Software Engineering I (02161)

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Software Engineering. Unit Testing Gobo Eiffel Test and Clover

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Annotations in Java (JUnit)

School of Informatics, University of Edinburgh

Lecture 5 Abstract Data Types

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

Transcription:

Tools for Unit Test JUnit Stuart Anderson

JUnit is a framework for writing tests JUnit 1 Written by Erich Gamma (Design Patterns) and Kent Beck (extreme Programming) JUnit uses Java s reflection capabilities (Java programs can examine their own code) and (as of version 4) annotations JUnit allows us to: define and execute tests and test suites Use test as an effective means of specification write code and use the tests to support refactoring integrate revised code into a build JUnit is available on several IDEs, e.g. BlueJ, JBuilder, and Eclipse have JUnit integration to some extent.

Slide 1: For more info on JUnit The JUnit site provides a wealth of useful information on JUnit and the host of JUnit-based products. http://www.junit.org/

1/2 2 JUnit s Terminology A test runner is software that runs tests and reports results. Many implementations: standalone GUI, command line, integrated into IDE A test suite is a collection of test cases. A test case tests the response of a single method to a particular set of inputs. A unit test is a test of the smallest element of code you can sensibly test, usually a single class.

2/2 3 JUnit s Terminology A test fixture is the environment in which a test is run. A new fixture is set up before each test case is executed, and torn down afterwards. Example: if you are testing a database client, the fixture might place the database server in a standard initial state, ready for the client to connect. An integration test is a test of how well classes work together. JUnit provides some limited support for integration tests. Proper unit testing would involve mock objects fake versions of the other classes with which the class under test interacts. JUnit does not help with this. necessary. It is worth knowing about, but not always

1/2 4 Structure of a JUnit (4) test class We want to test a class named Triangle This is the unit test for the Triangle class; it defines objects used by one or more tests. public class TriangleTestJ4{ } This is the default constructor. public TriangleTest(){ }

2/2 5 Structure of a JUnit (4) test class @Before public void init() Creates a test fixture by creating and initialising objects and values. @After public void cleanup() Releases any system resources used by the test fixture. Java usually does this for free, but files, network connections etc. might not get tidied up automatically. @Test public void nobadtriangles(), @Test public void scaleneok(), etc. These methods contain tests for the Triangle constructor and its isscalene() method.

1/2 6 Within a test, Making Tests: Assert Call the method being tested and get the actual result. assert a property that should hold of the test result. Each assert is a challenge on the test result. If the property fails to hold then assert fails, and throws an AssertionFailedError: JUnit catches these Errors, records the results of the test and displays them.

2/2 7 Making Tests: Assert static void asserttrue(boolean test) static void asserttrue(string message, boolean test) Throws an AssertionFailedError if the test fails. The optional message is included in the Error. static void assertfalse(boolean test) static void assertfalse(string message, boolean test) Throws an AssertionFailedError if the test succeeds.

Aside: Throwable 8 java.lang.error: a problem that an application would not normally try to handle does not need to be declared in throws clause. e.g. command line application given bad parameters by user. java.lang.exception: a problem that the application might reasonably cope with needs to be declared in throws clause. e.g. network connection timed out during connect attempt. java.lang.runtimeexception: application might cope with it, but rarely does not need to be declared in throws clause. e.g. I/O buffer overflow.

Example 9 Triangle class For the sake of example, we will create and test a trivial Triangle class: The constructor creates a Triangle object, where only the lengths of the sides are recorded and the private variable p is the longest side. The isscalene method returns true if the triangle is scalene. The isequilateral method returns true if the triangle is equilateral. We can write the test methods before the code. separating coding from testing. This has advantages in But Eclipse helps more if you create the class under test first: Creates test stubs (methods with empty bodies) for all methods and constructors.

Notes on creating tests 10 Size: Often the amount of (very routine) test code will exceed the size of the code for small systems. Complexity: Testing complex code can be a complex business and the tests can get quite complex. Effort: The effort taken in creating test code is repaid in reduced development time, most particularly when we go on to use the test subject in anger (i.e. real code). Behaviour: Creating a test often helps clarify our ideas on how a method should behave (particularly in exceptional circumstances).

Example 11 import junit.framework.testcase; public class TriangleTest extends TestCase { } private Triangle t; A JUnit 3 test for Triangle // Any method named setup will be executed before each test. protected void setup() { t = new Triangle(5,4,3); } protected void teardown() {} // teardown will be executed afterwards public void testisscalene() { // All tests are named test[something] asserttrue(t.isscalene()); } public void testisequilateral() { assertfalse(t.isequilateral()); }

Example 12 A JUnit 4 test for Triangle more imports are necessary R no need to inherit from TestCaseR package st; import static org.junit.assert.*; import org.junit.before; import org.junit.test; public class TestTriangle { Use annotations... R...rather than special names R } private Triangle t; @Before public void setup() throws Exception { t = new Triangle(3, 4, 5); } @Test public void scaleneok() { asserttrue(t.isscalene()); }

The Triangle class itself 13 Is JUnit too much for small programs? Not if you think it will reduce errors. Tests on this scale of program often turn up errors or omissions construct the tests working from the specification Sometimes you can omit tests for some particularly straightforward parts of the system

public class Triangle { private int p; // Longest edge private int q; private int r; public Triangle(int s1, int s2, int s3) { if (s1>s2) { p = s1; q = s2; } else { p = s2; q = s1; } if (s3>p) { r = p; p = s3; } else { r = s3; } } public boolean isscalene() { return ((r>0) && (q>0) && (p>0) && (p<(q+r))&& ((q>r) (r>q))); } public boolean isequilateral() { return p == q && q == r; } } Slide 13: The Triangle class itself

Assert methods II 14 assertequals(expected, actual) assertequals(string message, expected, actual) This method is heavily overloaded: expected and actual must be both objects or both of the same primitive type. For objects, uses your equals method, if you have defined it properly, as public boolean equals(object o) otherwise it uses == assertsame(object expected, Object actual) assertsame(string message, Object expected, Object actual) Asserts that two objects refer to the same object (using ==) assertnotsame(objectexpected, Objectactual) assertnotsame(string message, Object expected, Object actual) Asserts that two objects do not refer to the same object

Assert methods III 15 assertnull(object object) assertnull(string message, Object object) Asserts that the object is null assertnotnull(object object) assertnotnull(string message, Objectobject) Asserts that the object is null fail() fail(string message) Causes the test to fail and throw an AssertionFailedError Useful as a result of a complex test, when the other assert methods are not quite what you want

1/2 16 The assert statement in Java Earlier versions of JUnit had an assert method instead of an asserttrue method The name had to be changed when Java 1.4 introduced the assert statement There are two forms of the assert statement: assert boolean condition ; assert boolean condition : error message ; Both forms throw an AssertionFailedError if the boolean condition is false. The second form, with an explicit error message, is seldom necessary.

2/2 17 The assert statement in Java When to use an assert statement: Use it to document a condition that you know to be true Use assert false; in code that you know cannot be reached (such as a default case in a switch statement) Do not use assert to check whether parameters have legal values, or other places where throwing an Exception is more appropriate Can be dangerous: customers are not impressed by a library bombing out with an assertion failure.

To create a test class, select File New JUnit Test Case and enter the name of your test case JUnit in Eclipse Package R Test classr 18 Decide what stubs you want to creater Identify the class under test R

Creating a Test 19 Decide what you want to testr

Template for New Test 20

Running JUnit 21

Results 22 Results are herer

1/2 23 Issues with JUnit JUnit has a model of calling methods and checking results against the expected result. Issues are: State: objects that have significant internal state (e.g. collections with some additional structure) are harder to test because it may take many method calls to get an object into a state you want to test. Solutions: Write long tests that call some methods many times. Add additional methods in the interface to allow observation of state (or make private variables public?) Add additional methods in the interface that allow the internal state to be set to a particular value Heisenbugs can be an issue in these cases (changing the observations changes what is observed).

2/2 24 Issues with JUnit Other effects, e.g. output can be hard to capture correctly. JUnit tests of GUIs are not particularly helpful (recording gestures might be helpful here?)

Positives 25 Using JUnit encourages a testable style, where the result of a calling a method is easy to check against the specification: Controlled use of state Additional observers of the state (testing interface) Additional components in results that ease checking It is well integrated into a range of IDEs (e.g. Eclipse) Tests are easy to define and apply in these environments. JUnit encourages frequent testing during development e.g. XP (extreme Programming) test as specification JUnit tends to shape code to be easily testable. JUnit supports a range of extensions that support structured testing (e.g. coverage analysis) we will see some of these extensions later.

Framework for Integrated Test (FIT) 26 Another Framework for Testing Framework for Integrated Test (FIT), by Ward Cunningham (inventor of wiki) Allows closed loop between customers and developers: Takes HTML tables of expected behaviour from customers or spec. Turns those tables into test data: inputs, activities and assertions regarding expected results. Runs the tests and produces tabular summaries of the test runs. Only a few years old, but lots of people seem to like it various practitioners seem to think it is revolutionary.

Slide 26: Another Framework for Testing Framework for Integrated Test (FIT), is a tool which enhances communication between developers and users by allowing users to write tests in the form of structured (HTML) tables. For more info on FIT: http://fit.c2.com FitNesse hooks FIT up to a wiki, making collaborative development of tests even easier. http://fitnesse.org/

Required Readings Readings 27 JUnit Test Infected: Programmers Love Writing Tests an introduction to JUnit. Using JUnit With Eclipse IDE an O Reilly article Unit Testing in Jazz Using JUnit an NCSU Open Lab article on using JUnit with Eclipse Suggested Readings Michael Olan. 2003. Unit testing: test early, test often. J. Comput. Small Coll. 19, 2 (December 2003), 319-328.

Resources Getting started with Eclipse and JUnit Activity: to start using JUnit within Eclipe defining tests for a Triangle class. 28 review and try the example of [link to Activity] Video: this video tutorial shows how to create a new Eclipse project and start writing JUnit tests first. [link to Video]

Start up Eclipse and: Get testing! 29 1. Create a new Java project 2. Add a new package, st 3. Create st.triangle; grab the source from the Junit lecture s Activity in the resources 4. Create a new source folder called tests if you like (with a new st package) 5. Create a new JUnit test for st.triangle 6. And get testing! 7. Follow the video from the Junit lecture s resources for more details.