Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.

Slides:



Advertisements
Similar presentations
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Advertisements

Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
(1) Verification Methods CS2110: SW Development Methods Reading: Study Section in the MSD text These slides supplement the textbook – These touch.
Utilities (Part 3) Implementing static features 1.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Six compound procedures and higher-order procedures.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Fundamentals of Python: From First Programs Through Data Structures
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Fundamentals of Python: First Programs
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
Chapter 2 Additional Features of Java Programming.
Junit At the forefront of Test Driven Development.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Introduction to Software Testing. Types of Software Testing Unit Testing Strategies – Equivalence Class Testing – Boundary Value Testing – Output Testing.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Software Construction Lecture 18 Software Testing.
(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.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) Software Testing Department of Computer Science, Faculty.
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Utilities (Part 2) Implementing static features 1.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Dept of Computer Science University of Maryland College Park
Introduction to JUnit CS 4501 / 6501 Software Testing
CSE 374 Programming Concepts & Tools
Testing UW CSE 160 Winter 2017.
Exercise on Java Basics
Testing UW CSE 160 Spring 2018.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Building Java Programs
Building Java Programs
Testing UW CSE 160 Winter 2016.
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Building Java Programs
Programming in Java Assertion.
CS 1111 Introduction to Programming Fall 2018
Building Java Programs
Building Java Programs
Methods/Functions.
Junit Tests.
Presentation transcript:

Introduction to Testing 1

Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program testing can be a very effective way to show the presence of bugs, but it is hopelessly inadequate for showing their absence. —Edsger W. DijkstraEdsger W. Dijkstra  how can we test our utility class?  write a program that uses it and verify the result 2

3 public class IsThreeOfAKindTest { public static void main(String[] args) { // make a list of 5 dice that are 3 of a kind // check if Yahtzee.isThreeOfAKind returns true }

4 public class IsThreeOfAKindTest { public static void main(String[] args) { // make a list of 5 dice that are 3 of a kind List dice = new ArrayList (); dice.add(new Die(6, 1)); // 1 dice.add(new Die(6, 2)); // 2 dice.add(new Die(6, 3)); // 3 // check if Yahtzee.isThreeOfAKind returns true }

5 public class IsThreeOfAKindTest { public static void main(String[] args) { // make a list of 5 dice that are 3 of a kind List dice = new ArrayList (); dice.add(new Die(6, 1)); // 1 dice.add(new Die(6, 2)); // 2 dice.add(new Die(6, 3)); // 3 // check if Yahtzee.isThreeOfAKind returns true if (Yahtzee.isThreeOfAKind(dice) == true) { System.out.println("success"); }

6 public class IsThreeOfAKindTest { public static void main(String[] args) { // make a list of 5 dice that are 3 of a kind List dice = new ArrayList (); dice.add(new Die(6, 1)); // 1 dice.add(new Die(6, 2)); // 2 dice.add(new Die(6, 3)); // 3 // check if Yahtzee.isThreeOfAKind returns false if (Yahtzee.isThreeOfAKind(dice) == false) { throw new RuntimeException("FAILED: " + dice + " is a 3-of-a-kind"); }

Testing  we tested one case with a specific list of dice and an expected return value  the dice 1, 1, 1, 2, 3  expect method to return true  the method we are testing should return false if the list of dice does not contain a three-of-a-kind  we should test this, too 7

Testing  checking if a test fails and throwing an exception makes it easy to find tests that fail  because uncaught exceptions terminate the running program  unfortunately, stopping the test program might mean that other tests remain unrunnable  at least until you fix the broken test case 8

9 public class IsNotThreeOfAKindTest { public static void main(String[] args) { // make a list of 5 dice that are not 3 of a kind List dice = new ArrayList (); dice.add(new Die(6, 1)); // 1 dice.add(new Die(6, 6)); // 6 dice.add(new Die(6, 2)); // 2 dice.add(new Die(6, 3)); // 3 // check if Yahtzee.isThreeOfAKind returns true if (Yahtzee.isThreeOfAKind(dice) == true) { throw new RuntimeException("FAILED: " + dice + " is not a 3-of-a-kind"); }

Unit Testing  A unit test examines the behavior of a distinct unit of work. Within a Java application, the "distinct unit of work" is often (but not always) a single method. … A unit of work is a task that isn't directly dependent on the completion of any other task."  from the book JUnit in Action 10

Unit Testing: Test Cases  a unit test consists of one or more test cases  a test case for a method consists of  a specific set of arguments for the method  the expected output or result of the method 11

Unit Testing: Test Cases  example test case 1 for isThreeOfAKind  a specific set of arguments for the method  the list of dice with values 1, 1, 1, 2, 3  the expected output or result of the method  true  example test case 2 for isThreeOfAKind  a specific set of arguments for the method  the list of dice with values 1, 1, 6, 2, 3  the expected output or result of the method  false 12

Unit Testing: Test Cases  a test case passes if the actual output of the method matches the expected output of the method when the method is run with the test case arguments  it fails if the actual output does not match the expected output  typically, you use several test cases to test a method  the course notes uses the term test vector to refer to a collection of test cases  the term test suite is also commonly used 13

JUnit  JUnit is a testing framework for Java  JUnit provides a way for creating:  test cases  a class that contains one or more tests  test suites  a group of test cases  test runner  a way to automatically run test suites 14

15 import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import org.junit.Test; public class YahtzeeTest public void isThreeOfAKind() { // make a list of 5 dice that are 3 of a kind List dice = new ArrayList (); dice.add(new Die(6, 1)); // 1 dice.add(new Die(6, 2)); // 2 dice.add(new Die(6, 3)); // 3 assertTrue(Yahtzee.isThreeOfAKind(dice)); }

JUnit  our unit test tests if isThreeOfAKind produces the correct answer ( true ) if the list contains a three of a kind  we should also test if isThreeOfAKind produces the correct answer ( false ) if the list does not contain a three of a kind 16

public void notThreeOfAKind() { // make a list of 5 dice that are not 3 of a kind List dice = new ArrayList (); dice.add(new Die(6, 1)); // 1 dice.add(new Die(6, 6)); // 6 dice.add(new Die(6, 2)); // 2 dice.add(new Die(6, 3)); // 3 assertFalse(Yahtzee.isThreeOfAKind(dice)); }

JUnit  our unit tests use specific cases of rolls:  1, 1, 1, 2, 3 isThreeOfAKind  1, 1, 6, 2, 3 notThreeOfAKind  the tests don't tell us if our method works for different rolls:  3, 2, 1, 1, 1 ?  4, 6, 2, 3, 5 ?  can you write a unit test that tests every possible roll that is a three of a kind? every possible roll that is not three of a kind? 18

JUnit  notice that our test tests one specific three-of-a-kind  1, 1, 1, 2, 3  shouldn't we test all possible three-of-a-kinds?  or at least more three-of-a-kinds  how can you generate a list of dice that is guaranteed to contain three-of-a-kind? 19

JUnit  notice that our test tests one specific three-of-a-kind  1, 1, 6, 2, 3  shouldn't we test all possible not-three-of-a-kinds?  or at least more not-three-of-a-kinds  how can you generate a list of dice that is guaranteed to not contain three-of-a-kind? 20

Limitations of testing  who tests the tests?  it is often impossible or impractical to test all possible combinations of values for the method parameters  this means that the test implementer must choose specific test cases  it is very easy to miss test cases that would reveal an error  your code might contain an error even if your code passes all of the tests 21

Another testing example  consider the following utility class that checks if a numeric value lies inside a specified range 22

23 public class InRange { /** * Returns true if value is in the range [lo, hi]. * value is inside the range if and only if * lo <= value <= hi * lo the lower endpoint of the range hi the higher endpoint of the range value the value to check lo <= hi true if lo <= value <= hi, and false otherwise */ public static boolean isInRange(int lo, int hi, int value) { return value > lo && value < hi; }

Test cases  what test cases should we use to test our method? 24

Readings  the lectures have covered most of Chapter 2 of the course notes  up to and including all of Section 2.5  you should review the course notes to make sure you understand all of the material through Section 2.5  try to read Section 2.6 Beyond the Basics  some of the material from pages 34 and on are quite challenging 25