1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow

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

Java Testing Tools. junit is a testing harness for unit testing. emma is a code coverage tool. The tools can be used in concert to provide statement and.
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.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Approach of Unit testing with the help of JUnit Satish Mishra
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.
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.
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
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
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.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Sources  JUnit 4.0 documentation (from
Testowanie kodu Bartosz Baliś, Na podstawie prezentacji Satisha Mishra Iana Sommerville Erica Braude.
© 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.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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…
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
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
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Code Quality, Maintainability, Reusability, Debugging, Testing SIF8080, Sep. 27th 2001 Customer-driven project Carl-Fredrik Sørensen
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.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Software Testing, Debugging and JUnit Ananda Gunawardena Hao Cen.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Best Practices. Contents Bad Practices Good Practices.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Chapter 11, Testing: Unit Testing with JUnit 4
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-
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
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.
Operator Overloading Week 5.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
OOSC – Lab 1. Welcome Object Oriented programming is first and foremost a method for software construction, whose goal is to build high-quality software.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
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
Test-driven development (TDD)
Testing and debugging A short interlude 2-Dec-18.
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Testing and debugging A short interlude 17-Apr-19.
Learning JUnit for Unit Testing
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
CMPE212 – Reminders Assignment 2 due next Friday.
CS 240 – Advanced Programming Concepts
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow

2 Testing Not closely integrated with development.  prevents measurement of the progress of development.  can't tell when something starts working or when something stops working. JUnit to cheaply and incrementally build a test suite that helps to: – measure your progress, – spot unintended side effects. – focus your development efforts.

3 Testing with JUnit Automatic testing framework. – Acceptance tests. – Integration test. – Unit test. Reports the number of defects graphically. May create many tests for each method.

4 JUnit – Defines how to structure test cases – Provides the tools to run them. Implement a test as a subclass of TestCase.

5 JUnit Example Interplay of code and tests. – The style: 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. Example: Solve the problem of representing arithmetic with multiple currencies.

6 Example: Money class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount fCurrency= currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; }

7 Example: Money public Money add(Money m) { return new Money(amount()+m.amount(), currency()); }

8 JUnit Define MoneyTest as a subclass of TestCase. Put MoneyTest in the same package as the classes under test  access to the package private methods. – Add method testSimpleAdd, that will exercise the simple version of Money.add() above. – A JUnit test method is an ordinary method without any parameters.

9 Example: MoneyTest public class MoneyTest extends TestCase { //… public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF"); // (1) Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); // (2) assert(expected.equals(result)); // (3) }

10 Developing Tests public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); assert(!m12CHF.equals(null)); assertEquals(m12CHF, m12CHF); assertEquals(m12CHF, new Money(12, "CHF")); // (1) assert(!m12CHF.equals(m14CHF)); }

11 Developing Tests public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; } Override the method hashCode whenever you override method equals.

12 Assertions Verification by calling assert (inherited from TestCase). – Assert triggers a failure - logged by JUnit when the argument isn't true. – TestCase defines an assertEquals convenience method. Logs the printed value of the two objects if they differ. – Shows why a test failed in a JUnit test result report. Logged as a string representation created by toString.

13 Test Fixture public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); }

14 Tests Refactored public void testEquals() { assert(!f12CHF.equals(null)); assertEquals(f12CHF, f12CHF); assertEquals(f12CHF, new Money(12, "CHF")); assert(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); assert(expected.equals(result)); }

15 Running of Tests Two additional steps are needed to run the two test cases: 1. define how to run an individual test case, 2. define how to run a test suite. JUnit supports two ways of running single tests: – static. – dynamic.

16

17 JUnit Review (1) In general: development will go much smoother writing tests a little at a time when developing. When imagine of how the code will work, capture the thoughts in a test. Test code is just like model code in working  best if it is factored well.

18 JUnit Review (2) Keeping old tests running is just as important as making new ones run. The ideal is to always run all of your tests. When you are struck by an idea, defer thinking about the implementation. First write the test. Then run it. Then work on the implementation.

19 Testing Practices (1) Martin Fowler: "Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Only a fraction of the tests are actually useful. – Write tests that fail even though they should work, or tests that succeed even though they should fail. – Think of it is in cost/benefit terms. You want tests that pay you back with information.

20 Testing Practices (2) Receive a reasonable return on your testing investment: – During Development. – During Debugging. Caution: – Once you get them running, make sure they stay running. Ideally, run every test every time you change a method. Practically, the suite will soon grow too large to run all the time.

21 Testing Practices (3) Try to optimise your set-up code so you can run all the tests. Or, – create special suites that contain all the tests that might possibly be affected by your current development. – run the suite every time you compile. – make sure you run every test at least once a day: overnight, during lunch, during one of those long meetings….

22 References

23 Questions