JUnit Tutorial Hong Qing Yu Nov 2005.

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.
J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
Approach of Unit testing with the help of JUnit Satish Mishra
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 Syed Nabeel. Motivation Unit Testing Responsibility of  developer Rarely done properly Developers Excuse: “I am too much in a hurry”
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.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
JUnit Tutorial. Automating Testing Process Testing is boring and time-consuming, it is not a job for “human” Why not automating the testing process? Test.
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.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
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…
JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.
Programmer Testing Testing all things Java using JUnit and extensions.
Learning JUnit for Unit Testing JUnit Tutorial Dr. Robert L. Probert S.I.T.E., University of Ottawa Sept
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
INF 111 / CSE 121 Discussion Session Week 2 - Fall 2007 Instructor: Michele Rousseau TA: Rosalva Gallardo.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
1 Code Quality, Maintainability, Reusability, Debugging, Testing SIF8080, Sep. 27th 2001 Customer-driven project Carl-Fredrik Sørensen
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
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.
JUnit & Eclipse1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey.
Recursion. Midterm Exam Wednesday,November 4, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices Simple problem solving.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
CSC 480 Software Engineering Lecture 15 Oct 21, 2002.
JUnit Dwight Deugo Nesa Matic
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-
Tool Install How to download & install Java 6 & Eclipse updated version based on Dr. G. L. Ray’s slides.
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.
Introduction to Software Testing
© Spiros Mancoridis Software Engineering (Unit Testing Tools) Dependable Software Systems Topics in Unit Testing Tools Material drawn from [ junit.org,
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.
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.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
SE 433/333 Software Testing & Quality Assurance Dennis Mumaugh, Instructor Office: CDM, Room 428 Office Hours: Tuesday, 4:00 –
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.
JUnit Tatiana Totskaya. Main parts of the presentation  Unit Testing  JUnit – Main Concepts  JUnit Primer  Unit Testing in Eclipse Using JUnit.
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
JUnit Automated Software Testing Framework
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
An Automated Testing Framework
Overview of Eclipse Lectures
Test-driven development (TDD)
Unit Testing with JUnit
Introduction to Software Testing
Learning JUnit for Unit Testing
Joel Adams and Jeremy Frens Calvin College
Shane Cantrell Zach Crisman
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Junit Tests.
Chapter 20: Software Testing - Using JUnit, and Cobertura
Presentation transcript:

JUnit Tutorial Hong Qing Yu Nov 2005

JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices

The Testing Problems few programmers Why? Should write Do I am so busy It is difficult

The Testing Problems Programmers need such kind of tool: “Writing 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.” JUnit is that kind of tool!

JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices

The Framework of JUnit

JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices

A Case Study 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;     } public Money add(Money m) {     return new Money(amount()+m.amount(), currency()); } }

How to Write A TestCase (2) Exercises the objects in the fixture. 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.assertTrue(expected.equals(result));     // (3)     } (1) Creates the objects we will interact with during the test. This testing context is commonly referred to as a test's fixture. All we need for the testSimpleAdd test are some Money objects. (2) Exercises the objects in the fixture. (3) Verifies the result

Assert assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) assertEquals(message, expected, actual, delta) assertFalse(condition) assertFalse(message, condition) Assert(Not)Null(object) Assert(Not)Null(message, object) Assert(Not)Same(expected, actual) Assert(Not)Same(message, expected, actual) assertTrue(condition) assertTrue(message, condition)

Structure setUp() Storing the fixture's objects in instance variables of your TestCase subclass and initialize them by overriding the setUp method tearDown() Releasing the fixture’s run() Defining how to run an individual test case. Defining how to run a test suite. testCase()

Structure of Writing A Test public class MoneyTest extends TestCase {     private Money f12CHF;     private Money f14CHF;         protected void setUp() {         f12CHF= new Money(12, "CHF");         f14CHF= new Money(14, "CHF");     } public void testSimpleAdd() {     Money expected= new Money(26, "CHF");     Money result= f12CHF.add(f14CHF);     Assert.assertTrue(expected.equals(result)); } TestCase test= new MoneyTest("simple add") {     public void runTest() {         testSimpleAdd();     } }

Design Test Cases The real world scenarios The number boundaries Smaller than 0 such as –1, -2, …, -100, … Bigger than 0 such as 1, 2, …, 100…

The Feedback to Code 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;     } public Money add(Money m) { if (m.amount()<=0) throw exception;     return new Money(amount()+m.amount(), currency()); }

Test Case and Test Suite TestCase test= new MoneyTest("simple add") {     public void runTest() {         testSimpleAdd();     } public static Test suite() {     TestSuite suite= new TestSuite();     suite.addTest(new MoneyTest("testEquals"));     suite.addTest(new MoneyTest("testSimpleAdd"));     return suite;

Static and Dynamic Run JUnit supports two ways of running single tests: static dynamic TestCase test= new MoneyTest("simple add") {     public void runTest() {         testSimpleAdd();     } TestCase test= new MoneyTest("testSimpleAdd"); Since JUnit 2.0 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. public static Test suite() {  return new TestSuite(MoneyTest.class); }

JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices

JUnit

JUnit for Eclipse

Start to Use it 2. Installation unzip the junit.zip file 1. Download the latest version of JUnit from http://download.sourceforge.net/junit/ 2. Installation unzip the junit.zip file add junit.jar to the CLASSPATH. For example: set classpath=%classpath%;INSTALL_DIR\junit3\junit.jar 3. Testing Test the installation by using either the batch or the graphical TestRunner tool to run the tests that come with this release. All the tests should pass OK. for the batch TestRunner type:     java junit.textui.TestRunner junit.samples.AllTests for the graphical TestRunner type:     java junit.awtui.TestRunner junit.samples.AllTests for the Swing based graphical TestRunner type:     java junit.swingui.TestRunner junit.samples.AllTests Notice: The tests are not contained in the junit.jar but in the installation directory directly. Therefore make sure that the installation directory is on the class path Important: Don't install the junit.jar into the extension directory of your JDK installation. If you do so the test class on the files system will not be found. JUnit plug-in for Eclipse

Eclipse plug-in http://dev.eclipse.org/viewcvs/index.cgi/jdt-ui-home/plugins/org.eclipse.jdt.junit/org.eclipse.pde.junit_3.0.0.zip

The testing problems The framework of JUnit A case study JUnit tool Practices

Practices BankAccount case study

More Reading http://www.junit.org/index.htm http://open.ncsu.edu/se/tutorials/junit/ http://www.cs.umanitoba.ca/~eclipse/10-JUnit.pdf http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/junit/junit.pdf http://junit.sourceforge.net/javadoc/junit/framework/

Contact information hqy1@le.ac.uk Today’s slides can be find at http://www.cs.le.ac.uk/people/hqy1/JUnit%20Tutorial.ppt

Thanks And Questions