Software Testing Lecture 7 Property Based Testing. Justin Pearson

Similar documents
Software Testing Lecture 5. Justin Pearson

CSCI3390-Lecture 6: An Undecidable Problem

Lecture 27: Theory of Computation. Marvin Zhang 08/08/2016

Unit 4 Systems of Equations Systems of Two Linear Equations in Two Variables

CS154, Lecture 10: Rice s Theorem, Oracle Machines

CS 124 Math Review Section January 29, 2018

NP-Completeness. Until now we have been designing algorithms for specific problems

Math 308 Midterm Answers and Comments July 18, Part A. Short answer questions

Introduction to Computational Complexity

Essential facts about NP-completeness:

COMP 204. Object Oriented Programming (OOP) Mathieu Blanchette

Last Time. Inference Rules

Requirements Validation. Content. What the standards say (*) ?? Validation, Verification, Accreditation!! Correctness and completeness

1.1 P, NP, and NP-complete

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and

Definition: A "system" of equations is a set or collection of equations that you deal with all together at once.

Hypothesis testing I. - In particular, we are talking about statistical hypotheses. [get everyone s finger length!] n =

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

CS5371 Theory of Computation. Lecture 19: Complexity IV (More on NP, NP-Complete)

CSCI3390-Lecture 18: Why is the P =?NP Problem Such a Big Deal?

Finite Automata Theory and Formal Languages TMV027/DIT321 LP Recap: Logic, Sets, Relations, Functions

1 From previous lectures

CP405 Theory of Computation

Sporadic and related groups. Lecture 15 Matrix representations of sporadic groups.

Undecidability and Rice s Theorem. Lecture 26, December 3 CS 374, Fall 2015

Solving a Series. Carmen Bruni

Lecture 18: P & NP. Revised, May 1, CLRS, pp

Introduction. Pedro Cabalar. Department of Computer Science University of Corunna, SPAIN 2013/2014

NP-problems continued

Software Specification 2IX20

CS154, Lecture 17: conp, Oracles again, Space Complexity

Lecture 20: conp and Friends, Oracles in Complexity Theory

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Complexity Theory Part I

The Inductive Proof Template

Formal definition of P

P, NP, NP-Complete, and NPhard

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 6 Chapter 17 TRACTABLE AND NON-TRACTABLE PROBLEMS

Unit 6 Chapter 17 TRACTABLE AND NON-TRACTABLE PROBLEMS

Proving languages to be nonregular

ECS 120 Lesson 24 The Class N P, N P-complete Problems

2 Analogies between addition and multiplication

E.g. The set RE of regular expressions is given by the following rules:

CIS 2033 Lecture 5, Fall

Why Functional Programming Matters. John Hughes & Mary Sheeran

9 Nondeterministic Turing Machines

CSC 5170: Theory of Computational Complexity Lecture 9 The Chinese University of Hong Kong 15 March 2010

Chapter 2. Reductions and NP. 2.1 Reductions Continued The Satisfiability Problem (SAT) SAT 3SAT. CS 573: Algorithms, Fall 2013 August 29, 2013

Analyse et Conception Formelle. Lesson 4. Proofs with a proof assistant

Notes on Complexity Theory Last updated: October, Lecture 6

Space Complexity. The space complexity of a program is how much memory it uses.

Easy Problems vs. Hard Problems. CSE 421 Introduction to Algorithms Winter Is P a good definition of efficient? The class P

About the impossibility to prove P NP or P = NP and the pseudo-randomness in NP

Alan Bundy. Automated Reasoning LTL Model Checking

What is logic, the topic of this course? There are at least two answers to that question.

SAT, NP, NP-Completeness

Today s Topics. Methods of proof Relationships to logical equivalences. Important definitions Relationships to sets, relations Special functions

PHIL 422 Advanced Logic Inductive Proof

Intermediate Logic. Natural Deduction for TFL

First-Order Theorem Proving and Vampire. Laura Kovács (Chalmers University of Technology) Andrei Voronkov (The University of Manchester)

CSC236 Week 2. Larry Zhang

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Matroids and Greedy Algorithms Date: 10/31/16

Discrete Mathematics for CS Fall 2003 Wagner Lecture 3. Strong induction

P P P NP-Hard: L is NP-hard if for all L NP, L L. Thus, if we could solve L in polynomial. Cook's Theorem and Reductions

Symbolic Logic 3. For an inference to be deductively valid it is impossible for the conclusion to be false if the premises are true.

P is the class of problems for which there are algorithms that solve the problem in time O(n k ) for some constant k.

Tutorial on Mathematical Induction

NP-Completeness. NP-Completeness 1

Hoare Logic: Reasoning About Imperative Programs

Proof strategies, or, a manual of logical style

Business Statistics. Lecture 9: Simple Regression

V Honors Theory of Computation

Comp487/587 - Boolean Formulas

Knights, Knaves, and Logical Reasoning Mechanising the Laws of Thought

JASS 06 Report Summary. Circuit Complexity. Konstantin S. Ushakov. May 14, 2006

CSC 411 Lecture 6: Linear Regression

CSC173 Workshop: 13 Sept. Notes

Chapter Finding parse trees

30. TRANSFORMING TOOL #1 (the Addition Property of Equality)

Axiomatic Semantics. Operational semantics. Good for. Not good for automatic reasoning about programs

Factoring. there exists some 1 i < j l such that x i x j (mod p). (1) p gcd(x i x j, n).

1 Propositional Logic

ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER / Lines and Their Equations

2. Introduction to commutative rings (continued)

ITCS:CCT09 : Computational Complexity Theory Apr 8, Lecture 7

CHAPTER 8: EXPLORING R

Semantics and Generative Grammar. Formal Foundations: A Basic Review of Sets and Functions 1

Introduction to Computer Science and Programming for Astronomers

Lecture Notes on Compositional Reasoning

Complexity, P and NP

Harvard CS 121 and CSCI E-121 Lecture 22: The P vs. NP Question and NP-completeness

( )( b + c) = ab + ac, but it can also be ( )( a) = ba + ca. Let s use the distributive property on a couple of

Probability (Devore Chapter Two)

Lecture 2: Axiomatic semantics

IEOR 4106: Introduction to Operations Research: Stochastic Models Spring 2011, Professor Whitt Class Lecture Notes: Tuesday, March 1.

Lecture 22: PSPACE

Discrete Mathematics for CS Spring 2007 Luca Trevisan Lecture 27

Techniques for Proof Writing

8. TRANSFORMING TOOL #1 (the Addition Property of Equality)

Transcription:

Software Testing Lecture 7 Property Based Testing Justin Pearson 2017 1 / 13

When are there enough unit tests? Lets look at a really simple example. import u n i t t e s t def add ( x, y ) : return x+y c l a s s T e s t A d d i t i o n ( u n i t t e s t. TestCase ) : def t e s t a d d 1 ( s e l f ) : s e l f. a s s e r t E q u a l ( add ( 3, 4 ), 7 ) We could go on writing a lot of test cases. You can try to find edge cases and you could try to get some structural coverage of the code. 2 / 13

Random testing of addition def t e s t a d d r a n d o m s i n g l e ( ) : x = r a n d i n t ( s y s. maxsize, s y s. maxsize ) y = r a n d i n t ( s y s. maxsize, s y s. maxsize ) a s s e r t ( add ( x, y ) == x+y ) def t e s t a d d r a n d o m ( m a x i t e r a t i o n s ) : i = 0 ; while ( i <m a x i t e r a t i o n s ) : t e s t a d d r a n d o m s i n g l e ( ) i += 1 s y s. s t d o u t. w r i t e (. ) p r i n t ( \nran, m a x i t e r a t i o n s, t e s t s. ) 3 / 13

Random testing of addition We should not expect any of our tests for addition to fail. Especially since we implement addition with addition. But it does illustrate some important concepts. Test oracles: A mechanism for determining if a test has passed or not. Test generator: A mechanism for generating test cases. Some thought has to go into both oracles and generators. For generators you have to decide what sort of coverage you going to give. More on this later. 4 / 13

Set intersection The details are not important. I built a package 1 for representing sets of integers compactly as trees. Operations such as intersection were provided. Write inefficient but correct code: def i n t e r s e c t i o n ( l1, l 2 ) : n e w l i s t = [ ] f o r i i n l 1 : i f member ( i, l 2 ) : n e w l i s t. append ( i ) return ( n e w l i s t ) This is not a very efficient way of doing set intersection, but we can use it as an oracle together with randomly generated sets to generate lots of test cases. 1 https://bitbucket.org/justinkennethpearson/mdd 5 / 13

Writing Test Oracles It is the same problem as always, given some inputs how do we know what the correct output is. Often you use testing as a way to think about the problem and try understand what the output will be for given inputs. If you have lots of randomly generated tests then this is obviously not practical. There are a number of techniques available. Specification based verification of the outputs. Redundant computations Consistency checks. 6 / 13

Specification based Verification If you are lucky, then your software has a specification. If you less lucky, while trying to test the software and in dialogue with the programmer you write a specification. Given your specification there are a number of options. Use property based testing to derive test cases. More on this later. Use techniques from formal methods such as model checking, specification animation tools to generate test cases. This is often much easier than using formal methods to verify the software. Treat the specification as a combinatorial problem and use techniques such as constraint programming, SAT or SMT to model your problem and derive test cases (Take 1DL448). Write code the implements the specification and generates test cases. Techniques such as these are often referred to as model based testing. 7 / 13

Redundant Computations We have already seen two examples earlier. There are a number of options. Write inefficient but correct code, and use this as an oracle. Very similar to model based testing, but often more simple. Never underestimate the power of a random number generator and redundant computations; it can be a very good way of finding bugs in code. Parallel development of code. Very expensive only used in domains with high reliability requirements (aerospace). Has its own problems. Psychology tells us that people make the same sort of errors. 8 / 13

Consistency Checks Not really a test oracle, but often a good way to find errors. Make sure that the system satisfies any invariants. Make sure that the system ends up in a consistent state after you have done a bunch of operations. 9 / 13

Model Based Testing Models can come from a variety of sources: Finite state machines; UML designs; State charts; Or formal methods. Either you have to design your own models or adapt existing models so that they can be used to extract tests. There are various tools and techniques out there to extract test cases from models. 10 / 13

Model Based Testing Time spent modelling your problem is not wasted time. You have a way to generate lots of test cases, and you understand your problem better. 11 / 13

Property Based Testing Started with QuickCheck 2 which was a combinator library for describing test cases. Suppose we have a list reversing function then part of the specification is xs : list(int()) reverse(reverse(xs)) = xs Then we turn the specification into code (via macros)?forall(xs,list(int()), reverse(reverse(xs)) == Xs) 2 Koen Claessen and John Hughes (2000). QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs 12 / 13

Property Based Testing?FORALL takes three arguments: a variable, a generator and a boolean valued expression (the test oracle).?forall uses the generator to generate random instances, the boolean expression then run with the randomly generated instances. If it evaluates to false then we have found a failing test case. There is some clever logic to find reduce the size of the failing test case. Commercial and free implementations exists for many programming languages including Python. 13 / 13