JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt www.introsoftwaretesting.com Thanks in part to Aynur Abdurazik.

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

+ Introduction to JUnit IT323 – Software Engineering II By: Mashael Al-Duwais 1.
Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt aretest/ Second.
J-Unit Framework.
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.
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.
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.
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.
Ranga Rodrigo. Class is central to object oriented programming.
© 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.
Testing in Extreme Programming
Test automation / JUnit Building automatically repeatable test suites.
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.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
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 Automated Software Testing Framework Advanced Material Paul Ammann & Jeff Offutt
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 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
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.
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.
SE 433/333 Software Testing & Quality Assurance Dennis Mumaugh, Instructor Office: CDM, Room 428 Office Hours: Tuesday, 4:00 –
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.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Test automation / JUnit Building automatically repeatable test suites.
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
More JUnit CS 4501 / 6501 Software Testing
JUnit Automated Software Testing Framework
Introduction to Software Testing Chapter 3 Test Automation
Credit to Eclipse Documentation
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
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:

JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik

Introduction to Software Testing (Ch 1), © Ammann & Offutt 2 What is JUnit? Open source Java testing framework used to write and run repeatable automated tests A structure for writing test drivers JUnit features include: –Assertions for testing expected results –Test features for sharing common test data –Test suites for easily organizing and running tests –Graphical and textual test runners JUnit is widely used in industry JUnit can be used as stand alone Java programs (from the command line) or within an IDE such as Eclipse

Introduction to Software Testing (Ch 1), © Ammann & Offutt 3 JUnit Tests JUnit is used to test … –… an entire object –… part of an object – a method or some interacting methods –… interaction between several objects A tester class contains more than one test –Each test is written into one test method Test classes include : –A test runner to run the tests (main()) –A collection of test methods –Methods to set up the state before and update the state after each test and before and after all tests Get started at junit.org

Introduction to Software Testing (Ch 1), © Ammann & Offutt 4 Writing Tests for JUnit Need to use the methods of the junit.framework.assert class –javadoc gives a complete description of its capabilities Each test method checks a condition (assertion) and reports back to the test runner whether the test failed or succeeded The test runner uses the result to report to the user (in command line mode) or update the display (in an IDE) All of the methods return void A few representative methods of junit.framework.assert –assertTrue (boolean) –assertTrue (String, boolean) –assertEquals (Object, Object) –assertNull (Object) –Fail (String)

Introduction to Software Testing (Ch 1), © Ammann & Offutt 5 Sample Assertions static void assertEquals (boolean expected, boolean actual) Asserts that two booleans are equal static void assertEquals (byte expected, byte actual) Asserts that two bytes are equal static void assertEquals (char expected, char actual) Asserts that two chars are equal static void assertEquals (double expected, double actual, double delta) Asserts that two doubles are equal concerning a delta static void assertEquals (float expected, float actual, float delta) Asserts that two floats are equal concerning a delta static void assertEquals (int expected, int actual) Asserts that two ints are equal For a complete list, see –

Introduction to Software Testing (Ch 1), © Ammann & Offutt 6 How to Write A Test Case You may occasionally see old versions of JUnit tests –Major change in syntax and features in JUnit 4.0 –Backwards compatible (JUnit 3.X tests still work) 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 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 syntax introduced We will focus entirely on JUnit 4.X

Introduction to Software Testing (Ch 1), © Ammann & Offutt 7 JUnit Test Fixtures A test fixture is the state of the test –Objects and variables that are used by more than one test –Initializations (prefix values) –Reset values (postfix values) Different tests can use the objects without sharing the state Objects used in test fixtures should be declared as instance variables They should be initialized in method Can be deallocated or reset in method

Introduction to Software Testing (Ch 1), © Ammann & Offutt 8 Example JUnit Test Case public class Calc { static public long add (int a, int b) { return a + b; } import org.junit.Test Import static org.junit.Assert.*; public class CalcTest public void testAdd() { // Calc().add() returns long, // so we must cast 5 assertEquals ((long) 5, new Calc().add (2,3)); }

Introduction to Software Testing (Ch 1), © Ammann & Offutt 9 Testing the Immutable 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; }

Introduction to Software Testing (Ch 1), © Ammann & Offutt 10 Stack Test Class Classes to import : import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import junit.framework.JUnit4TestAdapter; Setup operations : private Stack stack; // setUp method syntax methods are run before each public void runBeforeEachTest() { stack = new Stack(); } Post-test operation : // tear-down method methods are run after each public void runAfterEachTest() { stack = null; }

Introduction to Software Testing (Ch 1), © Ammann & Offutt 11 Stack Test public void testToString() { stack = stack.push (new Integer (1)); stack = stack.push (new Integer (2)); assertEquals ("{2, 1}", stack.toString()); 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); } A problem with this test is that it actually combines four separate tests in one method Without automation, large tests have the advantage of reducing costs of running many tests With automation, small tests allow us to more easily identify failures …

Introduction to Software Testing (Ch 1), © Ammann & Offutt 12 Stack Test Cases public void testRepOkA() { boolean result = stack.repOk(); assertEquals (true, result); public void testRepOkB() { stack = stack.push (new Integer (1)); result = stack.repOk(); assertEquals (true, result); public void testRepOkC() { stack = stack.pop(); result = stack.repOk(); assertEquals (true, result); public void testRepOkD() { stack = stack.push (new Integer (1)); stack.top(); result = stack.repOk(); assertEquals (true, result); }

Introduction to Software Testing (Ch 1), © Ammann & Offutt 13 Running from a Command Line This is all that is needed to run JUnit in an IDE (like Eclipse) We need a main() for command line execution …

Introduction to Software Testing (Ch 1), © Ammann & Offutt 14 AllTests import org.junit.runner.RunWith; import org.junit.runners.Suite; import junit.framework.JUnit4TestAdapter; // This section declares all of the test classes in the ({ StackTest.class }) // Add test classes here. public class AllTests { // Execution begins at main(). In this test class, we will execute // a text test runner that will tell you if any of your tests fail. public static void main (String[] args) { junit.textui.TestRunner.run (suite()); } // The suite() method is helpful when using JUnit 3 Test Runners or Ant. public static junit.framework.Test suite() { return new JUnit4TestAdapter (AllTests.class); }

Introduction to Software Testing (Ch 1), © Ammann & Offutt 15 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 programmer to specify the test class to run Creates a “Run” button If a test fails, JUnit gives the location of the failure and any exceptions that were thrown

Introduction to Software Testing (Ch 1), © Ammann & Offutt 16 JUnit Resources Some JUnit tutorials – (Laurie Williams, Dright Ho, and Sarah Smith ) – (Sascha Wolski and Sebastian Hennebrueder) – (Diaspar software) – (Clarkware consulting) JUnit: Download, Documentation –

Introduction to Software Testing (Ch 1), © Ammann & Offutt 17 Summary The only way to make testing efficient as well as effective is to automate as much as possible JUnit provides a very simple way to automate our unit tests It is no “silver bullet” however … it does not solve the hard problem of testing : What tests to run ? This is test design … the purpose of test criteria JUnit also does not help with integration or system testing