3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Overview  What is a framework?  What is an Annotation?  JUnit.

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
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.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
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.
Object-Oriented Reengineering Patterns and Techniques Prof. O. Nierstrasz Prof. S. Ducasse T.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Overview  What is a framework?  JUnit — a simple testing framework.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Sources  JUnit documentation (from
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
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.
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.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Sources  JUnit 4.0 documentation (from
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 Unit Testing Explained How to support changes? How to support basic but synchronized documentation?
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.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 MakeMoney Corp. C*O of MakeMoney Corp. Our Vision  We invest in software  We do not know software 
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
A Unit Testing Framework: JUnit COMP 302 Software Engineering Koc University, Istanbul.
JUnitTest Infected: Programmers Love Writing Tests A little test, a little code, a little test, a little code…
Programmer Testing Testing all things Java using JUnit and extensions.
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.
Learning JUnit for Unit Testing JUnit Tutorial Dr. Robert L. Probert S.I.T.E., University of Ottawa Sept
© 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.
Principles of Object Oriented Programming Practical session 2 – part A.
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
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.
CSC 480 Software Engineering Lecture 15 Oct 21, 2002.
A tool for test-driven development
Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-
Program Testing, Debugging and JUnit. Program Development Process A simplified process –User requirement  Development  Delivery A simplified process.
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.
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.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
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.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Construction Lab 10 Unit Testing with JUnit
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
Software Engineering 1, CS 355 Unit Testing with JUnit
JUnit Automated Software Testing Framework
Test-driven development (TDD)
Learning JUnit for Unit Testing
JUnit SWE 619 Spring 2008.
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

3. A Testing Framework

© O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Overview  What is a framework?  What is an Annotation?  JUnit — a simple testing framework  Money and MoneyBag — a testing case study  Double Dispatch — how to add different types of objects  Testing practices Sources  JUnit 4.0 documentation (from

© O. Nierstrasz P2 — A Testing Framework 3.3 What you should know!  How does a framework differ from a library?  What is a unit test?  What is an annotation?  How does JUnit 3.x differ from JUnit 4.x  What is a test “fixture”?  What should you test in a TestCase?  How can testing drive design?  What is “double dispatch”? What does the name mean?

© O. Nierstrasz P2 — A Testing Framework 3.4 The Problem “Testing is not closely integrated with development. This prevents you from measuring the progress of development — you can't tell when something starts working or when something stops working.” Interactive testing is tedious and seldom exhaustive. Automated tests are better, but, —how to introduce tests interactively? —how to organize suites of tests?

© O. Nierstrasz P2 — A Testing Framework 3.5 Testing Practices During Development  When you need to add new functionality, write the tests first. —You will be done when the test runs.  When you need to redesign your software to add new features, refactor in small steps, and run the (regression) tests after each step. —Fix what’s broken before proceeding. During Debugging  When someone discovers a defect in your code, first write a test that demonstrates the defect. —Then debug until the test succeeds. “Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Martin Fowler

© O. Nierstrasz P2 — A Testing Framework 3.6 JUnit - A Testing Framework > JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks written by Kent Beck and Erich Gamma  For documentation of how to use JUnit

© O. Nierstrasz P2 — A Testing Framework 3.7 Frameworks vs. Libraries In traditional application architectures, user code makes use of library functionality in the form of procedures or classes: A framework reverses the usual relationship between generic and application code. Frameworks provide both generic functionality and application architecture: Essentially, a framework says: “Don’t call me — I’ll call you.” User Application main() User Application main() Library classes Framework classes main() Framework classes main() User classes

© O. Nierstrasz P2 — A Testing Framework 3.8 JUnit 3.8 JUnit is a simple “testing framework” that provides:  classes for writing Test Cases and Test Suites  methods for setting up and cleaning up test data (“fixtures”)  methods for making assertions  textual and graphical tools for running tests JUnit distinguishes between failures and errors:  A failure is a failed assertion, i.e., an anticipated problem that you test.  An error is a condition you didn’t check for, i.e., a runtime error.

© O. Nierstrasz P2 — A Testing Framework 3.9 The JUnit Framework

© O. Nierstrasz P2 — A Testing Framework 3.10 A Testing Scenario The framework calls the test methods that you define for your test cases.

© O. Nierstrasz P2 — A Testing Framework 3.11 JUnit 3.x Example Code import junit.framework.*; public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; public MoneyTest(String name) { super(name); } protected void setUp() {// create the test data f12CHF = new Money(12, "CHF"); f14CHF = new Money(14, "CHF"); } void testAdd() {// create the test data Money expected = new Money(26, “CHF”); assertEquals(“amount not equal”, expected,f12CHF.add(f14CHF); }... } import junit.framework.*; public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; public MoneyTest(String name) { super(name); } protected void setUp() {// create the test data f12CHF = new Money(12, "CHF"); f14CHF = new Money(14, "CHF"); } void testAdd() {// create the test data Money expected = new Money(26, “CHF”); assertEquals(“amount not equal”, expected,f12CHF.add(f14CHF); }... }

© O. Nierstrasz P2 — A Testing Framework 3.12 Annotations in J2SE 5  J2SE 5 introduces the Metadata feature (data about data)  Annotations allow you to add decorations to your code (remember javadoc )  Annotations are used for code documentation, compiler processing ), code generation, runtime processing  ( )

© O. Nierstrasz P2 — A Testing Framework 3.13 JUnit 4.x JUnit is a simple “testing framework” that provides:  Annotations for marking methods as tests  Annotations for marking methods that setting up and cleaning up test data (“fixtures”)  methods for making assertions  textual and graphical tools for running tests JUnit does not distinguish between failures and errors:  A failure is a failure is a failure

© O. Nierstrasz P2 — A Testing Framework 3.14 JUnit 4.x Example Code import junit.framework.*; import org.junit.*; import static org.junit.Assert.*; public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; public MoneyTest(String name) { super(name); public void setUp() {// create the test data f12CHF = new Money(12, "CHF"); // - the fixture f14CHF = new Money(14, "CHF"); public void testadd() {// create the test data Money expected = new Money(26, “CHF”); assertEquals(“amount not equal”, expected,f12CHF.add(f14CHF)); }... } import junit.framework.*; import org.junit.*; import static org.junit.Assert.*; public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; public MoneyTest(String name) { super(name); public void setUp() {// create the test data f12CHF = new Money(12, "CHF"); // - the fixture f14CHF = new Money(14, "CHF"); public void testadd() {// create the test data Money expected = new Money(26, “CHF”); assertEquals(“amount not equal”, expected,f12CHF.add(f14CHF)); }... }

© O. Nierstrasz P2 — A Testing Framework 3.15 Testing Style “The style here is to write a few lines of code, then a test that should run, or even better, to write a test that won't run, then write the code that will make it run.”  write unit tests that thoroughly test a single class  write tests as you develop (even before you implement)  write tests for every new piece of functionality “Developers should spend 25-50% of their time developing tests.”

© O. Nierstrasz P2 — A Testing Framework 3.16 Representing multiple currencies The problem... “The program we write will solve the problem of representing arithmetic with multiple currencies. Arithmetic between single currencies is trivial, you can just add the two amounts.... Things get more interesting once multiple currencies are involved.”

© O. Nierstrasz P2 — A Testing Framework 3.17 MoneyTest We start by defining a TestCase that exercises the interface we would like our Money class to support: import org.junit.*; import static org.junit.Assert.*; public class MoneyTest { private Money f12CHF; private Money f14CHF; public MoneyTest(String name) { super(name); public void setUp() {// create the test data f12CHF = new Money(12, "CHF"); f14CHF = new Money(14, "CHF"); }... } import org.junit.*; import static org.junit.Assert.*; public class MoneyTest { private Money f12CHF; private Money f14CHF; public MoneyTest(String name) { super(name); public void setUp() {// create the test data f12CHF = new Money(12, "CHF"); f14CHF = new Money(14, "CHF"); }... }

© O. Nierstrasz P2 — A Testing Framework 3.18 Some basic tests We define methods to test what we expect to be public void equals() { assertTrue(!f12CHF.equals(null)); assertEquals(f12CHF, f12CHF); assertEquals(f12CHF, new Money(12, "CHF")); assertTrue(!f12CHF.equals(f14CHF)); public void simpleAdd() { Money expected = new Money(26, "CHF"); Money result = f12CHF.add(f14CHF); assertEquals(expected, result); public void equals() { assertTrue(!f12CHF.equals(null)); assertEquals(f12CHF, f12CHF); assertEquals(f12CHF, new Money(12, "CHF")); assertTrue(!f12CHF.equals(f14CHF)); public void simpleAdd() { Money expected = new Money(26, "CHF"); Money result = f12CHF.add(f14CHF); assertEquals(expected, result); } NB: assertTrue is a static imported method of the Assert class of the JUnit 4.x Framework and raises an AssertionError. Junit 3.x raises a JUnit AssertionFailedError (!)

© O. Nierstrasz P2 — A Testing Framework 3.19 Money We now implement a Money class that fills our first few requirements: Note how the test case drives the design! NB: The first version does not consider how to add different currencies! public class Money {... public Money add(Money m) { return new Money(...); }... } public class Money {... public Money add(Money m) { return new Money(...); }... }

© O. Nierstrasz P2 — A Testing Framework 3.20 The TestRunner junit.ui.TestRunner is a GUI that we can use to instantiate and run the suite:

© O. Nierstrasz P2 — A Testing Framework 3.21 Running tests from eclipse >Add the junit.jar to the project’s buildpath libraries (Project>Properties…) >Create a JUnit test run configuration for your TestCase >Open a JUnit view and run the test.

© O. Nierstrasz P2 — A Testing Framework 3.22 Testing MoneyBags (I) To handle multiple currencies, we introduce a MoneyBag class that can hold several instances of Money: import static org.junit.Assert.*; public class MoneyTest public void setUp() { f12CHF = new Money(12, "CHF"); f14CHF = new Money(14, "CHF"); f7USD = new Money( 7, "USD"); f21USD = new Money(21, "USD"); fMB1 = new MoneyBag(f12CHF, f7USD); fMB2 = new MoneyBag(f14CHF, f21USD); } import static org.junit.Assert.*; public class MoneyTest public void setUp() { f12CHF = new Money(12, "CHF"); f14CHF = new Money(14, "CHF"); f7USD = new Money( 7, "USD"); f21USD = new Money(21, "USD"); fMB1 = new MoneyBag(f12CHF, f7USD); fMB2 = new MoneyBag(f14CHF, f21USD); }

© O. Nierstrasz P2 — A Testing Framework 3.23 Testing MoneyBags public void bagEquals() { assertTrue(!fMB1.equals(null)); assertEquals(fMB1, fMB1); assertTrue(!fMB1.equals(f12CHF)); assertTrue(!f12CHF.equals(fMB1)); assertTrue(!fMB1.equals(fMB2)); public void bagEquals() { assertTrue(!fMB1.equals(null)); assertEquals(fMB1, fMB1); assertTrue(!fMB1.equals(f12CHF)); assertTrue(!f12CHF.equals(fMB1)); assertTrue(!fMB1.equals(fMB2)); }... and define some new (obvious) tests...

© O. Nierstrasz P2 — A Testing Framework 3.24 MoneyBags We can use a HashTable to keep track of multiple Monies: class MoneyBag { private Hashtable fMonies = new Hashtable(5); MoneyBag(Money bag[]) { for (int i= 0; i < bag.length; i++) appendMoney(bag[i]); } private void appendMoney(Money aMoney) { Money m = (Money) fMonies.get(aMoney.currency()); if (m != null){ m = m.add(aMoney); } else{ m = aMoney; } fMonies.put(aMoney.currency(), m); } class MoneyBag { private Hashtable fMonies = new Hashtable(5); MoneyBag(Money bag[]) { for (int i= 0; i < bag.length; i++) appendMoney(bag[i]); } private void appendMoney(Money aMoney) { Money m = (Money) fMonies.get(aMoney.currency()); if (m != null){ m = m.add(aMoney); } else{ m = aMoney; } fMonies.put(aMoney.currency(), m); }

© O. Nierstrasz P2 — A Testing Framework 3.25 Testing MoneyBags (III) and we run the tests.

© O. Nierstrasz P2 — A Testing Framework 3.26 Adding MoneyBags We would like to freely add together arbitrary Monies and MoneyBags, and be sure that equals behave as equals: That implies that Money and MoneyBag should implement a common public void mixedSimpleAdd() { // [12 CHF] + [7 USD] == {[12 CHF][7 USD]} Money bag[] = { f12CHF, f7USD }; MoneyBag expected = new MoneyBag(bag); assertEquals(expected, f12CHF.add(f7USD)); public void mixedSimpleAdd() { // [12 CHF] + [7 USD] == {[12 CHF][7 USD]} Money bag[] = { f12CHF, f7USD }; MoneyBag expected = new MoneyBag(bag); assertEquals(expected, f12CHF.add(f7USD)); }

© O. Nierstrasz P2 — A Testing Framework 3.27 The IMoney interface (I) Monies know how to be added to other Monies Do we need anything else in the IMoney interface?

© O. Nierstrasz P2 — A Testing Framework 3.28 Double Dispatch (I) How do we implement add() without breaking encapsulation? “The idea behind double dispatch is to use an additional call to discover the kind of argument we are dealing with...” class Money implements IMoney {... public IMoney add(IMoney m) { return m.addMoney(this);// add me as a Money }... } class MoneyBag implements IMoney {... public IMoney add(IMoney m) { return m.addMoneyBag(this);// add as a MoneyBag }... } class Money implements IMoney {... public IMoney add(IMoney m) { return m.addMoney(this);// add me as a Money }... } class MoneyBag implements IMoney {... public IMoney add(IMoney m) { return m.addMoneyBag(this);// add as a MoneyBag }... }

© O. Nierstrasz P2 — A Testing Framework 3.29 Double Dispatch (II) The rest is then straightforward... and MoneyBag takes care of the rest. class Money implements IMoney {... public IMoney addMoney(Money m) { if (m.currency().equals(currency())) return new Money(amount()+m.amount(),currency()); else return new MoneyBag(this, m); } public IMoney addMoneyBag(MoneyBag s) { return s.addMoney(this); }... class Money implements IMoney {... public IMoney addMoney(Money m) { if (m.currency().equals(currency())) return new Money(amount()+m.amount(),currency()); else return new MoneyBag(this, m); } public IMoney addMoneyBag(MoneyBag s) { return s.addMoney(this); }...

© O. Nierstrasz P2 — A Testing Framework 3.30 The IMoney interface (II) So, the common interface has to be: public interface IMoney { public IMoney add(IMoney aMoney); IMoney addMoney(Money aMoney); IMoney addMoneyBag(MoneyBag aMoneyBag); } public interface IMoney { public IMoney add(IMoney aMoney); IMoney addMoney(Money aMoney); IMoney addMoneyBag(MoneyBag aMoneyBag); } NB: addMoney() and addMoneyBag() are only needed within the Money package.

© O. Nierstrasz P2 — A Testing Framework 3.31 A Failed test This time we are not so lucky...

© O. Nierstrasz P2 — A Testing Framework 3.32 The fix... It seems we forgot to implement MoneyBag.equals()! We fix it:... test it, and continue developing. class MoneyBag implements IMoney {... public boolean equals(Object anObject) { if (anObject instanceof MoneyBag) {... } else { return false; } class MoneyBag implements IMoney {... public boolean equals(Object anObject) { if (anObject instanceof MoneyBag) {... } else { return false; }

© O. Nierstrasz P2 — A Testing Framework 3.33 What you should know!  How does a framework differ from a library?  What is a unit test?  What is an annotation?  How does JUnit 3.x differ from JUnit 4.x  What is a test “fixture”?  What should you test in a TestCase?  How can testing drive design?  What is “double dispatch”? What does the name mean?

© O. Nierstrasz P2 — A Testing Framework 3.34 Can you answer these questions?  How does implementing toString() help in debugging?  How does the MoneyTest suite know which test methods to run?  How does the TestRunner invoke the right suite() method?  Why doesn’t the Java compiler complain that MoneyBag.equals() is used without being declared?

© O. Nierstrasz P2 — A Testing Framework 3.35 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.