Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.

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

Unit Testing Australian Development Centre Brisbane, Australia.
+ Introduction to JUnit IT323 – Software Engineering II By: Mashael Al-Duwais 1.
1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
J-Unit Framework.
An Introduction to JUnit Greg Jackson June Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp
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.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
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.
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.
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”
JUnit, Revisited 17-Apr-17.
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.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
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.
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.
CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2006 Doc 2 Terms & Testing Jan 24, 2006 Copyright ©, All rights reserved SDSU.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Programmer Testing Testing all things Java using JUnit and extensions.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Unit Testing Bartosz Walter Software Engineering Lecture XXX.
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.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
JUnit Dwight Deugo Nesa Matic
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
JUnit Don Braffitt Updated: 10-Jun-2011.
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. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
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.
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.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
JUnit, Bugzilla James Atlas July 24, 2008 *part of today’s slides courtesy of Dwight Deugo and Nesa Matic under the EPL.
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
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.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Test automation / JUnit Building automatically repeatable test suites.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
JUnit Automated Software Testing Framework
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Joel Adams and Jeremy Frens Calvin College
JUnit SWE 619 Spring 2008.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Presentation transcript:

Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah

JUnit  JUnit is a simple, open source framework to write and run repeatable tests. JUnit features include:  Assertions for testing expected results  Test fixtures for sharing common test data  Test runners for running tests  JUnit was originally written by Erich Gamma and Kent Beck.  Used for unit testing in Java. Other xUnit frameworks exist for other languages.  Supported by

Earlier Unit Testing  Use a debugger to go through code line by line.  Good to locate bugs.  Difficult for large system.  Impossible for regression testing.  Insert output statements in code  Easy to implement.  Rely on human eyes to recognize problems.  Hard to organize… causes the dreaded "Scroll Blindness".

Unit Testing  Programmers have aversion towards writing testing code  Too busy.  It is difficult to write and maintain.   Greater need to automate and simplify the process of writing unit tests

How Can JUnit help?  JUnit automates testing.  In fact, the organization and execution of testing.  JUnit saves 50% of your work, not all.  Still need to design test cases and implement it.  Fortunately, those are all needed to be done.  Write tests once, but run them as often needed.  Provides a well designed framework.  Abstracts common concerns of unit testing.  Easy to integrate test cases and test suites.

Test Method vs. Test Case  Test Case  A test case is usually a single run of a specific functionality.  Test Method  A test method can contain one or more test cases.  A test method usually has one or more assert statements or fail statements.  Test Suit  A collection of of test cases/classes executed together

Code Under Test  Suppose we want to implement a basic calculator program.  It has the general math operations- add, subtract, multiply, divide and square root.  Methods take a single parameter n and perform the operation with the stored value result. public class Calculator { private int result; public void add(int n) { result = result + n; } public void substract(int n) { result = result - n; }

Anatomy of the Test Class import junit.framework.*; public class CalculatorTest extends TestCase { public CalculatorTest(String testName) { super(testName); } protected void setUp() throws Exception { } protected void tearDown() throws Exception { } public void testAdd() { System.out.println("add"); int n = 0; Calculator instance = new Calculator(); instance.add(n); fail("The test case is a prototype."); }  The test class should extend junit.framework.TestCase  Tests must be public void  All test methods should have a name start with “test”  All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit.  If the method in the class under test is private, the test class must be in the same package.

Anatomy of the Test Class  import junit.framework.*;  The classes needed for using the assert methods.  public class CalculatorTest extends TestCase {  The use of the syntax prefixes can be avoided by extending the TestCase class.  setUp(), tearDown()  Run to do some common things that are done before and after each test  For example, in calculator example, we might like to reset the calculator to zero before and after testing each operation.  assert*() methods  Used to compare the obtained and expected results  The failure/success of the tests depend on their outcome.

Test Fixture  Useful if you have two or more tests for a common set of objects.  Avoids duplicating the code necessary to initialize (and cleanup) the common objects.  Tests don't share the state of objects in the test fixture.  setUp() and tearDown() methods can be used to define fixtures

junit.framework.Assert  Provide static methods which can help comparing the expected result and actual result.  If any assert is violated, a failure will be recorded. assertEquals (expected, actual)assertEquals (message, expected, actual) assertSame (expected, actual) assertSame (message, expected, actual) assertNotSame (unexpected, actual) assertNotSame (message, unexpected, actual) assertFalse (condition) assertFalse (message, condition) assertTrue (condition) assertTrue (message, condition) assertNotNull (object)assertNotNull (message, object) assertNull (object) assertNull (message, object) fail () fail (message)

Test Execution  Execute a test by using the Run function of the IDE.  NetBeans/Eclipse, can use a default test runner-- all the tests in the class run one by one. Alternatively,  Manually control the execution of the test program and select which test classes/methods to run by including the following method: public static Test suite( ) { TestSuite suite = new TestSuite( ); suite.addTest(new (“ ”) ); ………………………………… return suite; }

Status of a Test  A test is a single run of a test method.  Success  A test succeeds in time when No assert is violated; No fail statement is reached; No unexpected exception is thrown.  Failure  A test fails when an assert is violated or a fail statement is reached.  Error  An unexpected exception is thrown or timeout happens.

Status of a Test  On failure and error, the test results also show a stack trace of the execution.

References  LAB-5110 NetBeans™: JUnit (April 2005) (  Unit Testing in Eclipse Using JUnit by Laurie Williams, Dright Ho, and Sarah Smith (  JUnit Testing With Netbeans ( s&tt=junit&pf=y)  JUnit 4 Tutorial by Ji Chao Zhang, October 23, 2006 (CSI 5111 presentation) Based on “Get Acquainted with the New Advanced Features of JUnit 4” by Antonio Goncalves  JUnit Test Infected: Programmers Love Writing Tests; Kent Beck, Erich Gamma.  JUnit FAQ Edited by Mike Clark (