JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable.

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

J-Unit Framework.
Unit Testing. Topics Motivation JUnit framework Basic JUnit tests – static methods Ensure exceptions – instance methods.
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.
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.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
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.
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.
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.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Automated GUI testing How to test an interactive application automatically?
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.
Test automation / JUnit Building automatically repeatable test suites.
ITI 1120 Lab #7. Agenda Topics in this lab: –Methods & Arrays –Library classes –Testing (JUnit) –Strings In this lab, you are going to create your own.
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.
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.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
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. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
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.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt Updated.
SE 433/333 Software Testing & Quality Assurance Dennis Mumaugh, Instructor Office: CDM, Room 428 Office Hours: Tuesday, 4:00 –
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Test automation / JUnit Building automatically repeatable test suites.
Introduction to Unit Testing and JUnit David Rabinowitz.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
Software Construction Lab 10 Unit Testing with JUnit
Unit Testing with JUnit
CS427: Software Engineering I
CS427: Software Engineering I
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
Software Engineering 1, CS 355 Unit Testing with JUnit
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Advanced programming Practices
Test Driven Development
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.
Junit Tests.
Chapter 20: Software Testing - Using JUnit, and Cobertura
Presentation transcript:

JUnit SWE 619 Summer 2007

July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable automated tests JUnit features include:  Assertions for testing expected results  Test fixtures for sharing common test data  Test suites for easily organizing and running tests  Graphical and textual test runners

July 18, 2007 SWE 619 (c) Aynur Abdurazik 3 JUnit Architecture

July 18, 2007 SWE 619 (c) Aynur Abdurazik 4 Test Scenarios with JUnit Used to test  A whole object  Part of an object - a method or some interacting methods  Interaction between several objects A test case represents one scenario  A tester class contains more than one scenario  Each senario is written into one test method

July 18, 2007 SWE 619 (c) Aynur Abdurazik 5 Writing Tests for JUnit Need to use the methods of the junit.framework.assert class  javadoc gives a complete description of its capabilities Each of these methods checks some condition and reports back to the test runner whether the test failed or succeeded. The test runner uses the result to update the display. All of the methods return void (i.e. they are procedures)

July 18, 2007 SWE 619 (c) Aynur Abdurazik 6 Writing Tests for JUnit – (cont’d) A few representative methods of junit.framework.assert  assertTrue(boolean)  assertTrue(String, boolean)  assertEquals(Object, Object)  assertNull(Object)  fail(String)

July 18, 2007 SWE 619 (c) Aynur Abdurazik 7 Sample Assertions (cont’d) static void assertEquals(boolean expected, boolean actual) Asserts that two booleans are equal.assertEquals static void assertEquals(byte expected, byte actual) Asserts that two bytes are equal.assertEquals static void assertEquals(char expected, char actual) Asserts that two chars are equal.assertEquals static void assertEquals(double expected, double actual, double delta) Asserts that two doubles are equal concerning a delta.assertEquals static void assertEquals(float expected, float actual, float delta) Asserts that two floats are equal concerning a delta.assertEquals static void assertEquals(int expected, int actual) Asserts that two ints are equal.assertEquals For a complete list, see 

July 18, 2007 SWE 619 (c) Aynur Abdurazik 8 How to write test case Two different ways for junit versions 3.X and 4.x. In Junit 3.X  1) import junit.framework.* 2) extend TestCase.  3) name the test methods with a prefix of ‘test’  4) validate conditions using one of the several assert methods

July 18, 2007 SWE 619 (c) Aynur Abdurazik 9 How to write test case (cont’d) In Junit 4.0 and later:  Do not extend from Junit.framework.TestCase  Do not prefix the test method with ‘test’  Use one of the assert methods  Run the test using JUnit4TestAdapter

July 18, 2007 SWE 619 (c) Aynur Abdurazik 10 Simple Example public class Math { static public int add(int a, int b) { return a + b; } }

July 18, 2007 SWE 619 (c) Aynur Abdurazik 11 Example JUnit test case import junit.framework.*; public class TestMath extends TestCase { public void testAdd() { int num1 = 3; int num2 = 2; int total = 5; int sum = 0; sum = Math.add(num1, num2); assertEquals(sum, total); } }

July 18, 2007 SWE 619 (c) Aynur Abdurazik 12 Testing the Stack class public class Stack{ public String toString() { // EFFECTS: Returns the String representation of this Stack from the // top to the bottom. StringBuffer buf = new StringBuffer("{"); for (int i = size-1; i >= 0; i--) { if (i < (size-1)) { buf.append(", "); } buf.append(elements[i].toString()); } buf.append("}"); return buf.toString(); } public boolean repOk() { if (elements == null) return false; if (size != elements.length) return false; for (int i = 0; i < size; i++) { if (elements[i] == null) return false; } return true; }

July 18, 2007 SWE 619 (c) Aynur Abdurazik 13 TestCases with JUnit 4.X for Stack Necessary classes to be imported: import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import junit.framework.JUnit4TestAdapter;

July 18, 2007 SWE 619 (c) Aynur Abdurazik 14 TestCases with JUnit (Cont’d) public class StackTest { private Stack stack; /** * setUp method annotation. * Method can be named anything. public void runBeforeEachTest(){ stack = new Stack(); }

July 18, 2007 SWE 619 (c) Aynur Abdurazik 15 Test case with JUnit public void testRepOk(){ boolean result = stack.repOk(); assertEquals(true, result); stack = stack.push(new Integer(1)); result = stack.repOk(); assertEquals(true, result); stack = stack.pop(); result = stack.repOk(); assertEquals(true, result); stack = stack.push(new Integer(1)); stack.top(); result = stack.repOk(); assertEquals(true, result); }

July 18, 2007 SWE 619 (c) Aynur Abdurazik 16 Test case with JUnit public void testToString(){ stack = stack.push(new Integer(1)); stack = stack.push(new Integer(2)); assertEquals("{2, 1}", stack.toString()); public void runAfterEachTest(){ stack = null; } public static junit.framework.Test suite(){ return new JUnit4TestAdapter(StackTest.class); }

July 18, 2007 SWE 619 (c) Aynur Abdurazik 17 How to Run Tests JUnit provides test drivers  Character-based test driver runs from the command line  GUI-based test driver-junit.swingui.TestRunner allows to specify the test class to run, "Run" button examines the chosen class and finds all methods whose names begin with "test." If a test fails, Junit gives the location of the failure and any exceptions that were thrown

July 18, 2007 SWE 619 (c) Aynur Abdurazik 18 JUnit Installation in Eclipse In Eclipse, select Help->Software Updates->Find and Install Choose 'Search For New Features to Install' and select 'Next' Select 'New Remote Site' Enter a name for this server: JUnit Factory Enter (or copy/paste) this url: Install all plug-ins in the 'JUnit Factory' category and restart Eclipse

July 18, 2007 SWE 619 (c) Aynur Abdurazik 19 Generating Tests in Eclipse 1. Select a Java class in Eclipse and click the Generate Tests ( )button 2. When the JUnit Factory view says your test is done, click the link in the Result column to open it 3. Select the test and choose Run->Run As- >Agitar JUnit Test from the main menu

July 18, 2007 SWE 619 (c) Aynur Abdurazik 20 Eclipse JUnit Plugin See tutorial  