Lecture 6: Specifications & Testing

Similar documents
Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

Lecture 4. Defining Functions

Lecture 9: Memory in Python

Lecture 3: Functions & Modules

Lecture 9. Memory and Call Stacks

CS 1110, LAB 3: STRINGS; TESTING

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

CS1110. Lecture 6: Function calls

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

Lecture 17: Classes (Chapter 15)

Lecture 17: Classes (Chapter 15)

CS 1110, LAB 03: STRINGS; TESTING First Name: Last Name: NetID:

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Lecture 2: Variables & Assignments

CS 1110: Introduction to Computing Using Python Loop Invariants

Lecture 4: Defining Functions

Lecture 5: Strings

Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

Lecture 24: Loop Invariants

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Lecture 4. Defining Functions

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

CS1110. Lecture 6: Function calls

Lecture 20: While Loops (Sections 7.3, 7.4)

Lecture 10: Lists and Sequences

Lecture 5. Defining Functions

Lecture 4. Defining Functions

Conditionals & Control Flow

Lecture 11: Iteration and For-Loops

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

CS Lecture 19: Loop invariants

Lecture 19: Subclasses & Inheritance (Chapter 18)

PREPARING FOR PRELIM 1

Getting Started with Python

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Iteration and For Loops

Lecture 3. Functions & Modules

CSC148 Recipe for Designing Classes

CS Prelim 1 Review Fall 2018

Fundamentals of Programming (Python) Getting Started with Programming

CS 1110 SPRING 2016: LAB 3: PRACTICE FOR A2 (Feb 23-24)

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

CS1110 Lab 6 (Mar 17-18, 2015)

MIT AITI Python Software Development

CS 1110 Prelim 1 October 12th, 2017

Lecture 10. Memory in Python

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

Not-So-Mini-Lecture 6. Modules & Scripts

Lecture 3. Functions & Modules

Lecture 18: Using Classes Effectively (Chapter 16)

Last Name: First Name: Cornell NetID, all caps: CS 1110 Regular Prelim 1 Solutions March 2018

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

LAB 7 Writing Assembly Code

CS Prelim 1 Review Fall 2017

Announcements For This Lecture

CS 1110 Prelim 1 October 17th, 2013

Preconditions and Postconditions

CPSC 217-T03/T08. Functions Ruting Zhou

CS 115 Lecture 4. More Python; testing software. Neil Moore

CS 1110: Introduction to Computing Using Python Lists and Sequences

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Lecture 13. For-Loops

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

isinstance and While Loops

When Brunel s ship the SS Great Britain was launched into the River Thames, it made such a splash that several spectators on the opposite bank were

CS Prelim 1 Review Fall 2013

CS Lecture 26: Subclasses in Event-driven Programs A7 is out! Get started right away you need time to ask questions.

Lecture 13. For-Loops

Programming in the Real World. Dr. Baldassano Yu s Elite Education

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

CS Prelim 1 Review Fall 2012

Lecture 7. Memory in Python

Reminders. Lecture Outline. Lecture Agenda. Lecture Outline. Remember This? COMP10001 Foundations of Computing Testing and Debugging

Software Testing Lecture 1. Justin Pearson

COMP10001 Foundations of Computing Testing and Debugging

CS 1110 Prelim 1, March 2018

Measure for Measure! JSR-275 Public Review. Werner Keil. Santa Clara, California January 12th, Creative Arts & Technologies

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

Lecture 11. Asserts and Error Handling

CMSC 201 Spring 2017 Lab 05 Lists

CS Prelim 1 Review Fall 2016

Readings for This Lecture

CS 11 python track: lecture 3. n Today: Useful coding idioms

ECS15, Lecture 13. Midterm solutions posted. Topic 4: Programming in Python, cont. Staying up to speed with Python RESOURCES. More Python Resources

CSE : Python Programming

Exceptions & a Taste of Declarative Programming in SQL

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Testing. UW CSE 160 Winter 2016

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Lecture 1. Course Overview, Python Basics

CMSC 201 Spring 2019 Lab 06 Lists

CS1110 Lab 1 (Jan 27-28, 2015)

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Transcription:

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 6: Specifications & Testing (Sections 4.9, 9.5) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

What to do before the next class Download the code from lecture and run it. Better yet, try to write it yourself or modify it! Lab 3 starts today. You have two weeks to do it. Next week: no new lab. Wed. Feb 21 labs are drop-in office hours open to all. (Tue Feb 20 labs will not happen due to February break). Read Chapter 15 in the textbook. Starting this week: optional 1-on-1 with a staff member to help just you with course material. Sign up for a slot on CMS under the SPECIAL: one-on-ones. 2

Recall the Python API Function name https://docs.python.org/3/library/math.html Possible arguments Module What the function evaluates to This is a specification How to use the function Not how to implement it Write them as docstrings 3

Anatomy of a Specification def greet(name): """Prints a greeting to person name followed by conversation starter. <more details could go here> name: the person to greet Precondition: name is a string""" print('hello +name+! ) print('how are you? ) Short description, followed by blank line As needed, more detail in 1 (or more) paragraphs Parameter description Precondition specifies assumptions we make about the arguments 4

Anatomy of a Specification def get_campus_num(phone_num): """Returns the on-campus version of a 10-digit phone number. Returns: str of form X-XXXX Short description, followed by blank line Information about the return value Parameter description phone_num: number w/area code Precondition: phone_num is a 10 digit string of only numbers""" return phone_num[5]+"-"+phone_num[6:10] Precondition specifies assumptions we make about the arguments 5

A Precondition Is a Contract Precondition is met: The function will work! Precondition not met? Sorry, no guarantees Software bugs occur if: Precondition is not documented properly Function use violates the precondition Precondition violated: error! (bad!) Precondition violated: no error! (worse!) >>> get_campus_num( 6072554444 ) 5-4444 >>> get_campus_num( 6072531234 ) 3-1234 >>> get_campus_num(6072531234) Traceback (most recent call last): File "<stdin>", line 1, in<module> File "/Users/bracy/cornell_phone.py", line 12, in get_campus_num return phone_num[5]+"-"+phone_num[6:10] TypeError: 'int' object is not subscriptable >>> get_campus_num( 607-255-4444 ) 5-5-44 6

NASA Mars Climate Orbiter NASA lost a $125 million Mars orbiter because a Lockheed Martin engineering team used English units of measurement while the agency's team used the more conventional metric system for a key spacecraft operation... Sources: Wikipedia & CNN Source: NASA lost September 23, 1999 7

Preconditions Make Expectations Explicit In American terms: Preconditions help assign blame. Something went wrong. Did you use the function wrong? OR Was the function implemented/specified wrong? 8

Basic Terminology Bug: an error in a program. Expect them! Conceptual & implementation Debugging: the process of finding bugs and removing them Testing: the process of analyzing and running a program, looking for bugs Test case: a set of input values, together with the expected output Get in the habit of writing test cases for a function from its specification even before writing the function itself! 9

Test Cases help you find errors def vowel_count(word): """Returns: number of vowels in word. word: a string with at least one letter and only letters""" pass # nothing here yet! Some Test Cases vowel_count('bob ) Expect: 1 vowel_count('aeiuo ) Expect: 5 vowel_count('grrr ) Expect: 0 More Test Cases vowel_count('y ) Expect: 0? 1? vowel_count('bobo ) Expect: 1? 2? Test Cases can help you find errors in the specification as well as the implementation. 10

Representative Tests Cannot test all inputs Infinite possibilities Limit ourselves to tests that are representative Each test is a significantly different input Every possible input is similar to one chosen An art, not a science If easy, never have bugs Learn with much practice Representative Tests for vowel_count(w) Word with just one vowel For each possible vowel! Word with multiple vowels Of the same vowel Of different vowels Word with only vowels Word with no vowels 11

Representative Tests Example def last_name_first(full_name): """Returns: copy of full_name in form <last-name>, <first-name> full_name: has the form <first-name> <last-name> with one or more blanks between the two names""" end_first = full_name.find(' ') first = full_name[:end_first] last = full_name[end_first+1:] return last+', '+first Representative Tests: Look at precondition when choosing tests last_name_first( Maya Angelou ) Expects: Angelou, Maya' last_name_first( Maya Angelou ) Expects: 'Angelou, Maya' 12

cornellasserts module Contains useful testing functions To use: Download from: http://www.cs.cornell.edu/courses/cs1110/2018sp/lec tures/lecture06/modules/cornellasserts.py Put in same folder as the files you wish to test 13

Unit Test: A Special Kind of Script A unit test is a script that tests another module. It: Imports the module to be tested (so it can access it) Imports cornellasserts module (for testing) Defines one or more test cases that each include: A representative input The expected output Test cases use the cornellasserts function: def assert_equals(expected, received): """Quit program if expected and received differ""" 14

Testing last_name_first(full_name) import name import cornellasserts # The module we want to test # Includes the tests # First test case Actual output Input result = name.last_name_first('maya Angelou') cornellasserts.assert_equals('angelou, Maya', result) # Second test case result = name.last_name_first('maya Expected output Angelou') cornellasserts.assert_equals('angelou, Maya', result) print( All tests of the function last_name_first passed ) 15

Testing last_name_first(full_name) import name import cornellasserts # The module we want to test # Includes the tests # First test case result = name.last_name_first('maya Angelou') cornellasserts.assert_equals('angelou, Maya', result) # Second test case result = name.last_name_first('maya Angelou') Quits Python if not equal cornellasserts.assert_equals('angelou, Maya', result) Prints only if no errors print( All tests of the function last_name_first passed ) 16

Organizing your Test Cases We often have a lot of test cases Need a way to cleanly organize them Idea: Bundle all test cases into a single test! High level test: One of these for each function you want to test High level test performs all test cases for function Also uses some print statements (for feedback) 17

Bundling all the tests into a single test def test_last_name_first(): """Calls all the tests for last_name_first""" print('testing function last_name_first ) # Test 1 result = name.last_name_first('maya Angelou') cornellasserts.assert_equals('angelou, Maya', result) # Test 2 result = name.last_name_first('maya Angelou') cornellasserts.assert_equals('angelou, Maya', result) # Execution of the testing code test_last_name_first() No tests happen if you forget this print( All tests of the function last_name_first passed ) 18

Debugging with Test Cases (Question) def last_name_first(full_name): """Returns: copy of full_name in the form <last-name>, <first-name> 1 2 3 4 full_name: has the form <first-name> <last-name> with one or more blanks between the two names"" #get index of space after first name space_index = full_name.find(' ') #get first name first = full_name[:space_index] #get last name last = full_name[space_index+1:] #return <last-name>, <first-name> return last+', '+first last_name_first('maya Angelou ) Which line is wrong? A: Line 1 B: Line 2 C: Line 3 D: Line 4 E: I do not know gives 'Angelou, Maya' last_name_first('maya Angelou ) gives ' Angelou, Maya' 19

Debugging with Test Cases (Solution) 1 2 3 4 def last_name_first(full_name): """Returns: copy of full_name in the form <last-name>, <first-name> full_name: has the form <first-name> <last-name> with one or more blanks between the two names"" #get index of space after first name space_index = full_name.find(' ') #get first name first = full_name[:space_index] #get last name last = full_name[space_index+1:] #return <last-name>, <first-name> return last+', '+first last_name_first('maya Angelou ) Which line is wrong? A: Line 1 B: Line 2 C: Line 3 CORRECT D: Line 4 E: I do not know gives 'Angelou, Maya' last_name_first('maya Angelou ) gives ' Angelou, Maya' 20

How to debug Do not ask: Why doesn t my code do what I want it to do? Instead, ask: What is my code doing? Two ways to inspect your code: 1. Step through your code, drawing pictures (or use python tutor!) 2. Use print statements 21

Take a look in the python tutor! def last_name_first(full_name): <snip out comments for ppt slide> #get index of space space_index = full_name.find(' ') #get first name first = full_name[:space_index] #get last name last = full_name[space_index+1:] #return <last-name>, <first-name> return last+', '+first Pay attention to: Code you weren t 100% sure of as you wrote it Code relevant to the failed test case last_name_first( Maya Angelou ) 22

Using print statement to debug def last_name_first(full_name): print( full_name = +full_name) #get index of space space_index = full_name.find( ) print( space_index = + str(space_index)) #get first name first = full_name[:space_index] print( first = + first) #get last name last = full_name[space_index+1:] #return <last-name>, <first-name> print( last = + last) Sometimes this is your only option, but it does make a mess of your code, and introduces cut-n-paste errors. How do I print this? return last+', '+first 23