Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt aretest/ Second.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
Of 23 Leadership Rules SWE 301 / 401 Internship Preparation & Reflection Jeff Offutt
J-Unit Framework.
Java Review Interface, Casting, Generics, Iterator.
Chapter 10 Introduction to Arrays
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.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
ITEC200 Week02 Program Correctness and Efficiency.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Chapter 10 Classes Continued
Programmer Testing Testing all things Java using JUnit and extensions.
Dr. Pedro Mejia Alvarez Software Testing Slide 1 Software Testing: Building Test Cases.
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:
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Effective Java: Generics Last Updated: Spring 2009.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
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.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Introduction to Software Testing. Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Chapter 1 2.
Introduction to Software Testing Paul Ammann & Jeff Offutt Updated 24-August 2010.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
CPSC 873 John D. McGregor Session 9 Testing Vocabulary.
JUnit Automated Software Testing Framework Advanced Material Paul Ammann & Jeff Offutt
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
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,
Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt Updated.
CPSC 871 John D. McGregor Module 8 Session 1 Testing.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
Introduction to Software Testing Model-Driven Test Design and Coverage testing Paul Ammann & Jeff Offutt Update.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
CPSC 372 John D. McGregor Module 8 Session 1 Testing.
Testing: Fundamentals of Making a Good Test Presented by Emerson Murphy-Hill Based on Slides by ©Paul Ammann and Jeff Offutt
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Construction Lab 10 Unit Testing with JUnit
CS427: Software Engineering I
JUnit Automated Software Testing Framework
Test Automation CS 4501 / 6501 Software Testing
Introduction to JUnit CS 4501 / 6501 Software Testing
Input Space Partition Testing CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Testing and Debugging.
JUnit Automated Software Testing Framework
CMSC 202 Interfaces.
Introduction to Software Testing Chapter 3 Test Automation
Introduction to JUnit CS 4501 / 6501 Software Testing
Slides by Steve Armstrong LeTourneau University Longview, TX
Test Automation CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Test Driven Development
Test Driven Development
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
JUnit SWE 619 Spring 2008.
CMSC 202 Interfaces.
Presentation transcript:

Introduction to Software Testing (2nd edition) Chapter 3 Test Automation Paul Ammann & Jeff Offutt aretest/ Second version, 25 August 2013

What is Test Automation? Reduces cost Reduces human error Reduces variance in test quality from different individuals Significantly reduces the cost of regression testing Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt3 The use of software to control the execution of tests, the comparison of actual outcomes to predicted outcomes, the setting up of test preconditions, and other test control and test reporting functions

Software Testability (3.1) Plainly speaking – how hard it is to find faults in the software Testability is determined by two practical problems – How to provide the test values to the software – How to observe the results of test execution Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt4 The degree to which a system or component facilitates the establishment of test criteria and the performance of tests to determine whether those criteria have been met

Observability and Controllability Observability Observability – Software that affects hardware devices, databases, or remote files have low observability Controllability Controllability – Easy to control software with inputs from keyboards – Inputs from hardware sensors or distributed software is harder Data abstraction reduces controllability and observability Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt5 How easy it is to observe the behavior of a program in terms of its outputs, effects on the environment and other hardware and software components How easy it is to provide a program with the needed inputs, in terms of values, operations, and behaviors

Components of a Test Case (3.2) A test case is a multipart artifact with a definite structure Test case values Test case values Expected results Expected results Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt6 The result that will be produced when executing the test if the program satisfies it intended behavior The values that directly satisfy one test requirement

Affecting Controllability and Observability Prefix values Prefix values Postfix values Postfix values 1.Verification Values : Values needed to see the results of the test case values 2.Exit Commands : Values needed to terminate the program or otherwise return it to a stable state Executable test script Executable test script Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt7 Any inputs that need to be sent to the software after the test case values Any inputs necessary to put the software into the appropriate state to receive the test case values A test case that is prepared in a form to be executed automatically on the test software and produce a report

Test Automation Framework (3.3) Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt8 A set of assumptions, concepts and tools that support test automation

What is JUnit? Open source Java testing framework used to write and run repeatable automated tests JUnit is open source ( junit.org ) 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, Edition 2 (Ch 3) © Ammann & Offutt9

JUnit Tests JUnit can be used to test … – … an entire object – … part of an object – a method or some interacting methods – … interaction between several objects It is primarily for unit and integration testing, not system testing Each test is embedded into one test method A test class contains one or more test methods Test classes include : – 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, Edition 2 (Ch 3) © Ammann & Offutt10

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 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) – fail (String) Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt11

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 focus entirely on JUnit 4.X Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt12

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, Edition 2 (Ch 3) © Ammann & Offutt13

Simple JUnit Example Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt14 public class Calc { static public int add (int a, int b) { return a + b; } import org.junit.Test; import static org.junit.Assert.*; public class calcTest public void testAdd() { assertTrue (“Calc sum incorrect”, 5 == Calc.add (2, 3)); }

Testing the Min Class Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt15 import java.util.*; public class Min { /** * Returns the mininum element in a list list Comparable list of elements to search the minimum element in the list NullPointerException if list is null or * if any list elements are null ClassCastException if list elements are not mutually comparable IllegalArgumentException if list is empty */ … } public static > T min (List list) { if (list.size() == 0) { throw new IllegalArgumentException ("Min.min"); } Iterator itr = list.iterator(); T result = itr.next(); if (result == null) throw new NullPointerException ("Min.min"); while (itr.hasNext()) { // throws NPE, CCE as needed T comp = itr.next(); if (comp.compareTo (result) < 0) { result = comp; } } return result; }

MinTest Class Standard imports for all JUnit classes : Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt16 import static org.junit.Assert.*; import org.junit.*; import java.util.*; n Test fixture and pre-test setup method (prefix) : n Post test teardown method (postfix) : private List list; // Test fixture // Set up - Called before every test public void setUp() { list = new ArrayList (); } // Tear down - Called after every test public void tearDown() { list = null; // redundant in this example! }

Min Test Cases: NullPointerException Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & public void testForNullList() { list = null; try { Min.min (list); } catch (NullPointerException e) { return; } fail (“NullPointerException expected”); = NullPointerException.class) public void testForNullElement() { list.add (null); list.add ("cat"); Min.min (list); } This NullPointerException test uses the fail assertion This NullPointerException test decorates annotation with the class of the exception This NullPointerException test catches an easily overlooked special = NullPointerException.class) public void testForSoloNullElement() { list.add (null); Min.min (list); }

Remaining Test Cases for Min Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & = public void testMutuallyIncomparable() { List list = new ArrayList(); list.add ("cat"); list.add ("dog"); list.add (1); Min.min (list); = IllegalArgumentException.class) public void testEmptyList() { Min.min(list); } Note that Java generics don’t prevent clients from using raw types! Special case: Testing for the empty list Finally! A couple of “Happy Path” public void testSingleElement() { list.add ("cat"); Object obj = Min.min (list); assertTrue("Single Element List", obj.equals("cat")); public void testDoubleElement() { list.add ("dog"); list.add ("cat"); Object obj = Min.min (list); assertTrue ("Double Element List", obj.equals ("cat")); }

Data-Driven JUnit Tests Problem: Testing a function multiple times with similar values – How to avoid test code bloat? Simple example: Adding two numbers – Adding a given pair of numbers is just like adding any other pair – You really only want to write one test Data-driven unit tests call constructor for each logical set of data values – Same tests are then run on each set of data values – Collection of data values defined by method tagged annotation Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt19

Example JUnit D ata -D riven Unit Test Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt20 i mport org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import static org.junit.Assert.*; import (Parameterized.class) public class DataDrivenCalcTest { public int a, b, sum; public DataDrivenCalcTest (int a, int b, int sum) { this.a = a; this.b = b; this.sum = sum; public static Collection parameters() { return Arrays.asList (new Object [][] {{1, 1, 2}, {2, 3, 5}}); public void additionTest() { assertTrue ("Addition Test", sum == Calc.add (a,b)); } }

Tests with Parameters : JUnit Theories Unit tests can have actual parameters – So far, we’ve only seen parameterless test methods Contract model: Assume, act, Assert – Assumptions (preconditions) limit values appropriately – Action performs activity under scrutiny – Assertions (postconditions) check result Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & public void removeThenAddDoesNotChangeSet ( Set set, String string) { // Parameters! assumeTrue (set.contains (string)) ; // Assume Set copy = new HashSet (set); // Act copy.remove (string); copy.add (string); assertTrue (set.equals (copy)); // Assert // }

Question: Where Does The Data Come From? Answer: – All combinations of values annotations where assume clause is true – Four (of nine) combinations in this particular case – format is an array Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & public static String[] string = {"ant", "bat", public static Set[] sets = { new HashSet (Arrays.asList ("ant", "bat")), new HashSet (Arrays.asList (“bat", “cat", “dog“, “elk”)), new HashSet (Arrays.asList (“Snap”, “Crackle”, “Pop")) };

JUnit Theories Need BoilerPlate Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt23 import org.junit.*; import org.junit.runner.RunWith; import static org.junit.Assert.*; import static org.junit.Assume.*; import org.junit.experimental.theories.DataPoint; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import (Theories.class) public class SetTheoryTest { … // See Earlier Slides }

Running from a Command Line This is all we need to run JUnit in an IDE (like Eclipse) We need a main() for command line execution … Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt24

AllTests Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt25 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 in main(). This test class executes a // test runner that tells the tester if any fail. public static void main (String[] args) { junit.textui.TestRunner.run (suite()); } // The suite() method helpfs when using JUnit 3 Test Runners or Ant. public static junit.framework.Test suite() { return new JUnit4TestAdapter (AllTests.class); }

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, Edition 2 (Ch 3) © Ammann & Offutt26

JUnit Resources Some JUnit tutorials – (Laurie Williams, Dright Ho, and Sarah Smith ) – tutorial.html tutorial.html (Sascha Wolski and Sebastian Hennebrueder) – (Diaspar software) – (Clarkware consulting) JUnit: Download, Documentation – Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt27

Test Doubles (3.4) Actors use doubles to replace them during certain scenes – Dangerous or athletic scenes – Skills the actor doesn’t have, like dancing or singing – Partial nudity Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt28 n Test doubles replace software components that cannot be used during testing

Reasons for Test Doubles Component has not been written The real component does something destructive that we want to avoid during testing (unrecoverable actions) The real component interacts with an unreliable resource The real component runs very slowly The real component creates a test cycle –A depends on B, B depends on C, C depends on A Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt29 A test double is a software component that implements partial functionality to be used during testing

Test Double Illustration Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt30 Junit Test Double of Dependency Component Dependency Component Software Component Under Test Bomb Instantiate to test Use Calls

Types of Test Doubles 1. Dummy : Used to fill parameter lists 2. Fake : A working implementation that takes shortcuts – For example, an in-memory database 3. Stub : Hard-coded return values for the tests 4. Mock : Objects preprogrammed with preliminary specifications Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt31

Test Double Summary Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt32 These slides are preliminary As is Chapter 03 Still under editing and development September 2013

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 : Introduction to Software Testing, Edition 2 (Ch 3) © Ammann & Offutt33 What test values to use ? This is test design … the purpose of test criteria