S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.

Slides:



Advertisements
Similar presentations
J-Unit Framework.
Advertisements

MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
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.
Approach of Unit testing with the help of JUnit Satish Mishra
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.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
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.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
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.
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.
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.
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.
© 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.
Principles of Object Oriented Programming Practical session 2 – part A.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Testing in Extreme Programming
Automated GUI testing How to test an interactive application automatically?
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.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
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 Jumpstart © Manning Publications.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
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.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
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.
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 in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Unit Testing CSSE 514 Programming Methods 4/19/01.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
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.
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
Don Braffitt Updated: 26-Mar-2013
Unit testing Java programs Using JUnit
JUnit Automated Software Testing Framework
Selenium HP Web Test Tool Training
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Introduction to JUnit CS 4501 / 6501 Software Testing
Chapter 11, Testing.
Introduction to JUnit IT323 – Software Engineering II
Joel Adams and Jeremy Frens Calvin College
Java Remote Method Invocation
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:

S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University

S Ramakrishnan2 JUnit  Eric Gamma and Kent Beck created a simple & effective unit testing framework for Java called Junit in Junit in z part of XP (Extreme Programming method) but can be used independently. but can be used independently. zJunit (Framework) provides a reusable structure which can be shared between apps. which can be shared between apps. z Developers can incorporate this framework into their apps. and extend it to meet their specific their apps. and extend it to meet their specific requirements. requirements.

S Ramakrishnan3 JUnit  Junit (junit.org) is an open source software and is hosted on SourceForge. z defacto standard framework for developing unit tests in Java. z many unit testing frameworks available: Eiffel, PhP, Perl, ASP, C++, C# and …

S Ramakrishnan4 JUnit  A typical unit test: Check that a method accepts input in an expected range & returns the expected result value for each test input. z test the behaviour of a method through its interface. Test for expected values, also for exception z check if the method obeys the design by contract principles in terms of API contract.

S Ramakrishnan5 Unit testing frameworks z A simple Calculator class: z The core method, add: takes 2 doubles & returns the sum as a double public class Calculator { public double add(double num1, double num2) { return num1 + num2; } z Put in some work to test this simple “unit of work” will do the adding 2 doubles & returning the sum as a double correctly. And, also preserve this effort so that you can replicate this test and run the tests again later! z Objective test cases turned into reusable test cases -> Test Program

S Ramakrishnan6 Unit testing frameworks z A simple TestCalculator program – manual test public class TestCalculator { public static void main(String[ ] args) { Calculator calculator = new Calculator ( ); double result = calculator.add(10, 20); if (result != 30) { System.out.println(“Incorrect result: “ + result); } zThe test will pass when you compile & run this test program. If you change this code deliberately to make it fail, you are not testing the code! You should handle error conditions in Java by throwing exceptions.

S Ramakrishnan7 Unit testing frameworks z A simple TestCalculator program - Modified public class TestCalculator { private int errCount = 0; public void testAdd ( ); // moving the test into its own method { Calculator calculator = new Calculator ( ); double result = calculator.add(10, 20); if (result != 30) { throw new RuntimeException(“Incorrect result: “ + result); } public static void main (String [ ] args) { TestCalculator test = new TestCalculator ( ); try { test.testAdd ( ); // can add more methods like testAdd with more unit tests in the test calculator & invoke them here } catch (Throwable e) { test.errCount++; e.printStackTrace ( ); } if (test.errCount > 0) { throw new RuntimeException(“Number of Errors : “ + test.errCount “ ); }

S Ramakrishnan8 Unit testing frameworks z Lessons from the last simple example re: unit testing frameworks: Each unit test must run independently of all other unit tests must be easy to define which unit tests will run errors must be detected and reported for each test separately. The modified vers.(last slide) can be improved. Each unit test must be independent & run in a different classloader instance.

S Ramakrishnan9 Unit testing frameworks z A better option is to consider a unit test suite. problem with this approach: large try/catch block can be a maintenance headache Another approach is to use Java’s reflection & introspection features. A program looks at itself & decide to run methods which are named in a certain way, eg. those begin with letters, test. The Junit framework supports introspecting methods, supports the use of a different classloader instance for each test and reports errors for each test separately.

S Ramakrishnan10 Testing with JUnit Create a test script with a number of small java methods the idea is to create java objects, do something with these and check if the objects exhibit correct properties Assertions methods to check properties such as identity of objects, equality of variables can check if objects are null or non-null; equality of objects (via == or equals () depending on the type) use assertions to determine the verdict of the test case. In XP methodology, a JUnit test should be written first before any code, and executed. the implementation code should be written to get the test to pass re-execute the test with this code and it should pass.

S Ramakrishnan11 Junit TestCase (Usually, a test case is considered to be a single test that can pass or fail, and a testsuite is a bunch of related testcases). In Junit, run multiple test cases withTestSuite & Testcase. They are implemented as classes. No difference between running a testsuite & a testcase. A TestCase is a class and a single test is a method. TestCalculator program written with Junit import junit.framework.TestCase; // Junit needs it to automatically run the tests public class TestCalculator extends TestCase; { public void testAdd ( ); // method name follows the pattern testXXX { Calculator calculator = new Calculator ( ); // start the test by creating an instance (object under test) double result = calculator.add(10, 20); // execute the test by calling the method to test, passing it known values assetEquals ( 30, result, 0); // check the result by calling assertEquals method, inherited from base class, TestCase. } Javadoc for assertEquals method /** *Asserts that two doubles are equal concerning a delta. … */ Static public void assertEquals (double expected, double actual, double delta) In the above code, assertEquals is passed: expected = 30, actual = result, delta = 0 Often, delta parameter can be zero and can be ignored. It is used when floating point calc. are done. Delta provides a + / - factor. If the actual data is within the range of expected +/- delta, the test passes.

S Ramakrishnan12 Junit TestCase public void run(TestResult result) // run method in TestCase Class { result.startTest (this); setUp ( ); // set up for all tests in class try { runTest ( ); // run the test } catch (AssertionFailedError e) { result.addFailure (this, e); } catch (Throwable e) { result.addError (this, e); } finally { tearDown ( ); // cleanup for all tests in class }

S Ramakrishnan13 The Junit Framework TestResult Test TestCaseTestSuite yourTestClass-1 yourTestClass-n run(TestResult) runTest ( ) setup ( ) tearDown ( ) run(TestResult) fName fTests *suite ( ): TestSuite

S Ramakrishnan14 Junit workings Junit uses reflection to build the testsuite dynamically use getClass ( ) on an object to know which class it belongs to use getMethods ( ) to find out the methods in a class use invoke ( ) on each method to run it This ensures that test suite need not be updated if a method is added or deleted from the test suite. Test class is re loaded each time the tests are run this means that JUnit test execution window need not be restarted if test cases are recompiled To run a Test object - either a TestCase or a TestSuite, simply invoke the run ( ) method.

S Ramakrishnan15 Reference Junit web site: