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.

Slides:



Advertisements
Similar presentations
Introduction to Programming
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.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Introduction to Computing Science and Programming I
Debugging Introduction to Computing Science and Programming I.
Chapter 6: User-Defined Functions I
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
CS 201 Functions Debzani Deb.
Chapter 15 Testing. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Why Testing? In chapter 14, we.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Python quick start guide
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Lists in Python.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Testing Michael Ernst CSE 140 University of Washington.
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.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
CPS120 Introduction to Computer Science Iteration (Looping)
Which program is better? Why? (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n d)
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Exceptions and assertions CSE 331 University of Washington.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
CS101 Computer Programming I Chapter 4 Extra Examples.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 CS161 Introduction to Computer Science Topic #9.
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
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.
TESTING FUNDAMENTALS BY K.KARTHIKEYAN.
Controlling Program Flow with Decision Structures.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4
Which program is better? Why?
Testing Tutorial 7.
Testing UW CSE 160 Winter 2017.
CSS 161: Fundamentals of Computing
User-Defined Functions
Testing UW CSE 160 Spring 2018.
Cracking the Coding Interview
Higher-Order Procedures
CSE 105 theory of computation
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 15 Debugging.
Testing UW CSE 160 Winter 2016.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
CS 1111 Introduction to Programming Fall 2018
Chapter 15 Debugging.
CSE 1020:Software Development
Chapter 3 Debugging Section 3.4
Review of Previous Lesson
CSE 105 theory of computation
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Defensive Programming
Presentation transcript:

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 far more important than speed 2

Famous examples Ariane 5 rocket Therac-25 radiation therapy machine 3

Testing does not prove correctness Edsger Dijkstra: “Program testing can be used to show the presence of bugs, but never to show their absence!” 4

Testing = double-checking results How do you know your program is right? – Compare its output to a correct output How do you know a correct output? – Real data is big – You wrote a computer program because it is not convenient to compute it by hand Use small inputs so you can compute by hand Example: standard deviation – What are good tests for std_dev ? 5

Testing ≠ debugging Testing: determining whether your program is correct – Doesn’t say where or how your program is incorrect Debugging: locating the specific defect in your program, and fixing it 2 key ideas: – divide and conquer – the scientific method 6

What is a test? A test consists of: – an input (sometimes called “test data”) – an oracle (a predicate (boolean expression) of the output) Example test for sum: – input: [1, 2, 3] – oracle: result is 6 – write the test as: sum([1, 2, 3]) == 6 Example test for sqrt: – input: 3.14 – oracle: result is within of – ways to write the test: sqrt(3.14) – < sqrt(3.14) – < math.abs(sqrt(3.14) – 1.772) <

Test results The test passes if the boolean expression evaluates to True The test fails if the boolean expression evaluates to False Use the assert statement: assert sum([1, 2, 3]) == 6 assert math.abs(sqrt(3.14) – 1.772) < assert True does nothing assert False crashes the program – and prints a message 8

Where to write test cases At the top level: is run every time you load your program def hypotenuse(a, b): … assert hypotenuse(3, 4) == 5 assert hypotenuse(5, 12) == 13 In a test function: is run when you invoke the function def hypotenuse(a, b): … def test_hypotenuse(): assert hypotenuse(3, 4) == 5 assert hypotenuse(5, 12) == 13 9

Assertions are not just for test cases Use assertions throughout your code Documents what you think is true about your algorithm Lets you know immediately when something goes wrong – The longer between a code mistake and the programmer noticing, the harder it is to debug 10

Assertions make debugging easier Common, but unfortunate, course of events: – Code contains a mistake (incorrect assumption or algorithm) – Intermediate value (e.g., in local variable, or result of a function call) is incorrect – That value is used in other computations, or copied into other variables – Eventually, the user notices that the overall program produces a wrong result – Where is the mistake in the program? It could be anywhere. Suppose you had 10 assertions evenly distributed in your code – When one fails, you can localize the mistake to 1/10 of your code (the part between the last assertion that passes and the first one that fails) 11

Where to write assertions Function entry: are arguments legal? – Place blame on the caller before the function fails Function exit: is result correct? Places with tricky or interesting code Assertions are ordinary statements; e.g., can appear within a loop: for n in myNumbers: assert type(n) == int or type(n) == float 12

Where not to write assertions Don’t clutter the code – (Same rule as for comments) Don’t write assertions that are certain to succeed – The existence of an assertion tells a programmer that it might possibly fail Don’t write an assertion if the following code would fail informatively assert type(name) == str … "Hello, " + name … Write assertions where they may be useful for debugging 13

What to write assertions about Results of computations Correctly-formed data structures assert 0 <= index < len(mylist) assert len(list1) == len(list2) 14

When to write tests Two possibilities: – Write code first, then write tests – Write tests first, then write code It’s best to write tests first If you write the code first, you remember the implementation while writing the tests – You are likely to make the same mistakes in the implementation If you write the tests first, you will think more about the functionality than about a particular implementation – You might notice some aspect of behavior that you would have made a mistake about 15

Write the whole test A common mistake: 1.Write the function 2.Make up test inputs 3.Run the function 4.Use the result as the oracle You didn’t write a test, but only half of a test – Created the tests inputs, but not the oracle The test does not determine whether the function is correct – Only determines that it continues to be as correct (or incorrect) as it was before 16

Tests are for specified behavior def roots(a, b, c): """Returns a list of the two roots of ax**2 + bx + c."""... Bad test of implementation-specific behavior: assert roots(1, 0, -1) == [1, -1] Assertions inside a routine can be for implementation-specific behavior 17

Tests prevent you from introducing errors when you change a function Abstraction: the implementation details do not matter Preventing introducing errors when you make a change is called “regression testing” 18

Write tests that cover all the functionality Think about and test “corner cases” – Empty list – Zero – int vs. float values 19

Testing Approaches Black box testing - Choose test data without looking at implementation Glass box (white box, clear box) testing - Choose test data with knowledge of implementation 20

def isPrime(x): """Assumes x is a nonnegative int Returns True if x is prime; False otherwise""" if x <= 2: return False for i in range(2, x): if x%i == 0: return False return True 21

Tests might not reveal an error def mean(numbers): """Returns the average of the argument list. The argument must be a non-empty list of numbers.""" return sum(numbers)/len(numbers) # Tests assert mean([1, 2, 3, 4, 5]) == 3 assert mean([1, 2.1, 3.2]) == 2.1 This implementation is elegant, but wrong! mean([1,2,3,4]) 22

Don’t write meaningless tests def mean(numbers): """Returns the average of the argument list. The argument must be a non-empty list of numbers.""" return sum(numbers)/len(numbers) Unnecessary tests. Don’t write these: mean([1, 2, "hello"]) mean("hello") mean([]) 23