Software Engineering

Similar documents
Software Engineering Testing and Debugging Testing

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

Lecture 15 Software Testing

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Chapter 8 Software Testing. Chapter 8 Software testing

Assertions. Assertions - Example

Regression testing. Whenever you find a bug. Why is this a good idea?

Programming Embedded Systems

Testing, Debugging, and Verification

CSE 331 Final Exam 3/16/15 Sample Solution

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Verification and Validation. Ian Sommerville 2004 Software Engineering, 7th edition. Chapter 22 Slide 1

Topics in Software Testing

So, You Want to Test Your Compiler?

CSE 331 Software Design and Implementation. Lecture 3 Loop Reasoning

MSO Lecture Design by Contract"

Unit Testing as Hypothesis Testing

Testing is a very big and important topic when it comes to software development. Testing has a number of aspects that need to be considered.

No Source Code. EEC 521: Software Engineering. Specification-Based Testing. Advantages

Lecture 4: Procedure Specifications

Software Engineering

Testing, Debugging, Program Verification

Black Box Testing. EEC 521: Software Engineering. Specification-Based Testing. No Source Code. Software Testing

Write for your audience

From designing to coding

Object Oriented Software Design - I

Software Engineering

Verification and Validation

Steps for project success. git status. Milestones. Deliverables. Homework 1 submitted Homework 2 will be posted October 26.

Chapter 8 Software Testing. Chapter 8 So-ware tes0ng

Unit Testing as Hypothesis Testing

Quality Assurance in Software Development

Testing. CMSC 433 Programming Language Technologies and Paradigms Spring A Real Testing Example. Example (Black Box)?

Software Engineering

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

CS 161 Computer Security

Software Engineering and Scientific Computing

CITS5501 Software Testing and Quality Assurance Formal methods

Classes, interfaces, & documentation. Review of basic building blocks

Program Correctness and Efficiency. Chapter 2

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Code Contracts in C#

LINEAR INSERTION SORT

Software Engineering I (02161)

hwu-logo.png 1 class Rational { 2 int numerator ; int denominator ; 4 public Rational ( int numerator, int denominator ) {

Computer Science and Software Engineering University of Wisconsin - Platteville 9-Software Testing, Verification and Validation

Verification and Validation. Assuring that a software system meets a user s needs. Verification vs Validation. The V & V Process

Software Quality. Richard Harris

Software Testing Lecture 1. Justin Pearson

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Verification and Validation

Program Verification! Goals of this Lecture! Words from the Wise! Testing!

Software Testing. Software Testing. in the textbook. Chapter 8. Verification and Validation. Verification and Validation: Goals

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

Testing, Debugging, and Verification

CSE 403: Software Engineering, Fall courses.cs.washington.edu/courses/cse403/16au/ Unit Testing. Emina Torlak

Object-Oriented Software Engineering Practical Software Development using UML and Java

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

CSE 331 Final Exam 12/14/15 Sample Solution. Question 1. (20 points, 1 each) Warmup. For each statement, circle T if it is true and F if it is false.

Bridge Course On Software Testing

Principles of Software Construction: Objects, Design, and Concurrency (Part 2: Designing (Sub )Systems)

CSE 15L Midterm Summer 2011

CS 520 Theory and Practice of Software Engineering Fall 2018

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

A Practical Approach to Programming With Assertions

Testing. UW CSE 160 Winter 2016

17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

a correct statement? You need to know what the statement is supposed to do.

High Quality Java Code: Contracts & Assertions

CPS122 Lecture: Detailed Design and Implementation

CS 307: Software Engineering. Lecture 10: Software Design and Architecture

Formal Methods for Java

Part 5. Verification and Validation

Memory Safety (cont d) Software Security

C++ for Java Programmers

Static program checking and verification

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Testing and Debugging Debugging

Chapter 8 Software Testing

Today s Topic. Software Engineering Testing and Debugging Debugging. Today s Topic. The Main Steps in Systematic Debugging

CPSC 427a: Object-Oriented Programming

Chapter 1: Principles of Programming and Software Engineering

Software Test. Levels of test. Types of test. Regression test The JUnit tool for unit testing Java programs. System test Integration test Unit test

Software Design Models, Tools & Processes. Lecture 6: Transition Phase Cecilia Mascolo

Introduction to JML David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

TEST-DRIVEN DEVELOPMENT

Code Documentation.

Static Analysis in Practice

Unit Testing and Test Driven Design

School of Informatics, University of Edinburgh

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

Type Checking in COOL (II) Lecture 10

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Advances in Programming Languages

Lecture 20: SW Testing Presented by: Mohammad El-Ramly, PhD

Software LEIC/LETI. Lecture 5

Fundamentals of Software Engineering

Transcription:

Software Engineering Lecture 13: Testing and Debugging Testing Peter Thiemann University of Freiburg, Germany SS 2014

Recap

Recap Testing detect the presence of bugs by observing failures

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash)

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash) Writing a specification is difficult

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash) Writing a specification is difficult Finding a good set of test cases is often difficult

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash) Writing a specification is difficult Finding a good set of test cases is often difficult Absence of failing tests does not imply absence of defects

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash) Writing a specification is difficult Finding a good set of test cases is often difficult Absence of failing tests does not imply absence of defects But making testing part of the development makes claims about the software more credible

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash) Writing a specification is difficult Finding a good set of test cases is often difficult Absence of failing tests does not imply absence of defects But making testing part of the development makes claims about the software more credible Program Verification guarantees absence of defects

Recap Testing detect the presence of bugs by observing failures Debugging find the defect causing a certain failure To decide when a program fails we need a specification (except for obvious failure such as a program crash) Writing a specification is difficult Finding a good set of test cases is often difficult Absence of failing tests does not imply absence of defects But making testing part of the development makes claims about the software more credible Program Verification guarantees absence of defects Preferable, but verification is often economically impossible

Contents of Testing part

Contents of Testing part Specifications (informal)

Contents of Testing part Specifications (informal) Test Cases

Contents of Testing part Specifications (informal) Test Cases How to write a test case

Contents of Testing part Specifications (informal) Test Cases How to write a test case How to build a good test suite (collection of test cases)

Specifications Specification determines what a piece of code should do what it requires to do its job A specification is a contract between the implementor and the user of the implemented code. A specification consists of two parts: Requires (precondition) what the user should fulfill before calling the code Ensures (postcondition) what the implementor promises about the result of the execution (provided requires were fulfilled)

Specification Example 1 public static int find_min ( int [] a) {... }

Specification Example 1 public static int find_min ( int [] a) {... } Specification Requires: Ensures: Result is the minimum element in a

Specification Example 1 public static int find_min ( int [] a) {... } Specification Requires: a is non-null Ensures: Result is the minimum element in a

Specification Example 1 public static int find_min ( int [] a) {... } Specification Requires: a is non-null Ensures: Result is a minimum element in a

Specification Example 1 public static int find_min ( int [] a) {... } Specification Requires: a is non-null Ensures: Result is equal to the minimum element in a

Specification Example 1 public static int find_min ( int [] a) {... } Specification Requires: a is non-null Ensures: Result is less than or equal to all elements in a

Specification Example 1 public static int find_min ( int [] a) {... } Specification Requires: a is non-null Ensures: Result is less than or equal to all elements in a and equal to at least one element in a

Specification Example 1 public static int find_ min ( int [] a) { 2 int x, i; 3 x = a [0]; 4 for (i = 1; i < a. length ;i ++) { 5 if (a[i] < x) x = a[i]; 6 } 7 return x; 8 } Specification Requires: a is non-null Ensures: Result is less than or equal to all elements in a and equal to at least one element in a

Specification Example 1 public static int find_ min ( int [] a) { 2 int x, i; 3 x = a [0]; 4 for (i = 1; i < a. length ;i ++) { 5 if (a[i] < x) x = a[i]; 6 } 7 return x; 8 } Specification Requires: a is non-null and contains at least one element Ensures: Result is less than or equal to all elements in a and equal to at least one element in a

What can be wrong about a specification?

What can be wrong about a specification? Badly stated does not make sense Specification Requires: a is non-null Ensures: Result is the minimum element in a

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Specification Requires: a is non-null Ensures: Result is a minimum element in a

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Incomplete more can be said about the behaviour

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Incomplete more can be said about the behaviour Postcondition is too weak Specification Requires: a is non-null Ensures: Result is less than or equal to all elements in a

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Incomplete more can be said about the behaviour Postcondition is too weak Precondition is too strong Specification Requires: a is non-null and contains exactly one element Ensures: Result is less than or equal to all elements in a and equal to at least one element in a

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Incomplete more can be said about the behaviour Postcondition is too weak Precondition is too strong Incorrect

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Incomplete more can be said about the behaviour Postcondition is too weak Precondition is too strong Incorrect Precondition is too weak Specification Requires: a is non-null Ensures: Result is less than or equal to all elements in a and equal to at least one element in a

What can be wrong about a specification? Badly stated does not make sense Vague unclear what is meant Incomplete more can be said about the behaviour Postcondition is too weak Precondition is too strong Incorrect Precondition is too weak Postcondition is too strong Specification Requires: a is non-null and contains at least one element Ensures: Result is less than or equal to all elements in a and equal to at least one element in a, and result is greater than 0

What can go wrong when writing specifications? Are all these cases of bad specifications? We don t want incorrect specifications. A vague specification is open to misunderstanding. An imcomplete specification can be useful.

Example: Strong or Weak Precondition Example What does this method do? 1 public static int [] insert ( int [] x, int n) 2 { 3 int [] y = new int [x. length + 1]; 4 int i; 5 for (i = 0; i < x. length ; i ++) { 6 if (n <= x[i]) break ; 7 y[i] = x[i]; 8 } 9 y[i] = n; 10 for (; i < x. length ; i ++) { 11 y[i +1] = x[i]; 12 } 13 return y; 14 }

Example, cont d Example What does this method do? 1 public static int [] insert ( int [] x, int n) 2 {... } Specification Requires: Ensures:

Example, cont d Example What does this method do? 1 public static int [] insert ( int [] x, int n) 2 {... } Specification Requires: x is non-null. Ensures: Result is equal to x with n inserted in it.

Example, cont d Example What does this method do? 1 public static int [] insert ( int [] x, int n) 2 {... } Specification Requires: x is non-null. Ensures: Result is equal to x with n inserted in it and result is sorted in ascending order.

Example, cont d Example What does this method do? 1 public static int [] insert ( int [] x, int n) 2 {... } Specification Requires: x is non-null and sorted in ascending order. Ensures: Result is equal to x with n inserted in it and result is sorted in ascending order.

Specification of a Class Class invariant A class invariant is a condition about the state of each class instance that should be maintained throughout its existence We will focus on weak invariants It should hold between calls to methods of the class, but not during the execution of such methods

Specification of a Class Class invariant A class invariant is a condition about the state of each class instance that should be maintained throughout its existence We will focus on weak invariants It should hold between calls to methods of the class, but not during the execution of such methods Class specification consists of Class invariant Requires and ensures of the methods

Example, class invariant 1 public class HashSet { 2 private Object [] arr ; 3 int nobj ; 4 5 public void insert ( Object o) {... } 6... 7 }

Example, class invariant 1 public class HashSet { 2 private Object [] arr ; 3 int nobj ; 4 5 public void insert ( Object o) {... } 6... 7 } Class Invariant

Example, class invariant 1 public class HashSet { 2 private Object [] arr ; 3 int nobj ; 4 5 public void insert ( Object o) {... } 6... 7 } Class Invariant nobj should be equal to the number of non-null elements in arr, and

Example, class invariant 1 public class HashSet { 2 private Object [] arr ; 3 int nobj ; 4 5 public void insert ( Object o) {... } 6... 7 } Class Invariant nobj should be equal to the number of non-null elements in arr, and for each index i in range of arr such that arr[i] is non-null, all elements between indices arr[i].hash() and i are non-null, and

Example, class invariant 1 public class HashSet { 2 private Object [] arr ; 3 int nobj ; 4 5 public void insert ( Object o) {... } 6... 7 } Class Invariant nobj should be equal to the number of non-null elements in arr, and for each index i in range of arr such that arr[i] is non-null, all elements between indices arr[i].hash() and i are non-null, and there are no two non-null elements of arr that are equal

Testing

Software testing What we look at here Systematic general rules for how to write test cases Repeatable be able to run tests over and over again

Software testing What we look at here Systematic general rules for how to write test cases Repeatable be able to run tests over and over again What we don t look at here Running a program to see if anything goes wrong Letting a lot of people run the program to see if anything goes wrong (Beta-testing)

Testing on Different Levels Unit Testing testing a small unit of a system Requires that the behaviour of the unit has been specificied. In Java this often corresponds to testing a method or a class.

Testing on Different Levels Unit Testing testing a small unit of a system Requires that the behaviour of the unit has been specificied. In Java this often corresponds to testing a method or a class. Integration Testing testing the interaction between two or more units

Testing on Different Levels Unit Testing testing a small unit of a system Requires that the behaviour of the unit has been specificied. In Java this often corresponds to testing a method or a class. Integration Testing testing the interaction between two or more units System Testing testing a whole system against the specification of its externally observable behaviour System testing is mostly useful for convincing about the correctness. Less useful for finding bugs because the infection phase going from defect to failure is usually complex and difficult to unwind.

Testing on Different Levels Unit Testing testing a small unit of a system Requires that the behaviour of the unit has been specificied. In Java this often corresponds to testing a method or a class. Integration Testing testing the interaction between two or more units System Testing testing a whole system against the specification of its externally observable behaviour System testing is mostly useful for convincing about the correctness. Less useful for finding bugs because the infection phase going from defect to failure is usually complex and difficult to unwind. The code that is being tested is called the IUT (implementation under test).

Testing Non-Functional Requirements Not considered further Performance testing or load testing Stability testing Usability testing Security testing

When Should Testing Take Place? The sooner a bug is found, the better. Testing should start early. Extreme case: Test-driven program development

When Should Testing Take Place? The sooner a bug is found, the better. Testing should start early. Extreme case: Test-driven program development but Testing early means a lot of unit testing which requires a lot of specifications. But writing specifications for the units of a system is already needed for a large project when programming by contract.

When Should Testing Take Place? The sooner a bug is found, the better. Testing should start early. Extreme case: Test-driven program development but Testing early means a lot of unit testing which requires a lot of specifications. But writing specifications for the units of a system is already needed for a large project when programming by contract. Tested units may be replaced later on, making the tests useless. On the other hand, writing and running tests often gives a deep understanding of the program. The need to replace the unit may have been realized during the testing activities.

Systematic testing Use precise methods to design correct tests

Systematic testing Use precise methods to design correct tests Each individual test is called a test case

Systematic testing Use precise methods to design correct tests Each individual test is called a test case Organize collections of related test cases in test suites

Systematic testing Use precise methods to design correct tests Each individual test is called a test case Organize collections of related test cases in test suites Use precise methods to make sure that a test suite has a good coverage of the different cases of usage

Repeatable testing The basic idea is to write code that performs the tests.

Repeatable testing The basic idea is to write code that performs the tests. A tool can automatically run a large collection of tests

Repeatable testing The basic idea is to write code that performs the tests. A tool can automatically run a large collection of tests The testing code can be integrated into the actual code, thus stored in an organised way

Repeatable testing The basic idea is to write code that performs the tests. A tool can automatically run a large collection of tests The testing code can be integrated into the actual code, thus stored in an organised way When a bug has been fixed, the tests can be rerun to check if the failure is gone

Repeatable testing The basic idea is to write code that performs the tests. A tool can automatically run a large collection of tests The testing code can be integrated into the actual code, thus stored in an organised way When a bug has been fixed, the tests can be rerun to check if the failure is gone Whenever the code is extended, all old test cases can be rerun to check that nothing is broken (regression testing)

Repeatable testing The basic idea is to write code that performs the tests. A tool can automatically run a large collection of tests The testing code can be integrated into the actual code, thus stored in an organised way When a bug has been fixed, the tests can be rerun to check if the failure is gone Whenever the code is extended, all old test cases can be rerun to check that nothing is broken (regression testing) JUnit is a tool for organizing, writing, and running test cases. It provides: Functionality that is needed repeatedly when writing test cases A way to annotate methods as test cases A way to run test cases automatically

Testing influences code quality Testing should result in removal of defects

Testing influences code quality Testing should result in removal of defects When writing specifications and test cases for units, the responsibilities of the different parts become clearer, which promotes good OO programming style (low coupling)

Testing influences code quality Testing should result in removal of defects When writing specifications and test cases for units, the responsibilities of the different parts become clearer, which promotes good OO programming style (low coupling) To enable automatic program testing, it is important to separate IO and functionality (design for testability)

What does a test case consist of? Test case (Set up) Initialisation (of class instance and input arguments) Call to the method of the IUT A test oracle which decides if the test succeeded or failed (Tear down) Finalization; relinquish resources etc

What does a test case consist of? Test case (Set up) Initialisation (of class instance and input arguments) Call to the method of the IUT A test oracle which decides if the test succeeded or failed (Tear down) Finalization; relinquish resources etc The test oracle is vital to run tests automatically

Demo Small demo showing basics of how to use JUnit

Summary, and what s next? Summary Specifications (motivation, contracts, pre- and postconditions, what to think about) Testing (motivation, different kinds of testing, role in software development, junit) What s next? More examples of test cases, presenting aspects of writing test cases and features of JUnit How to write a good test case? How to construct a good collection of test cases (test suite)?