Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz...
Transcript of Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz...
![Page 1: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/1.jpg)
Specifications & Testing
Lecture 6
![Page 2: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/2.jpg)
Announcements For This Lecture
Last Call• Acad. Integrity Quiz• Take it by tomorrow• Also remember survey
Assignment 1• Posted on web page
§ Due Sun, Sep. 17th
§ Due in place of Lab 4§ Revise until correct
• Can work in pairs§ One submission for pair§ Mixer Tue 5:30 meeting
in Phillips 203
29/7/17 Specifications & Testing
![Page 3: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/3.jpg)
One-on-One Sessions
• Starts today: 1/2-hour one-on-one sessions§ To help prepare you for the assignment§ Primarily for students with little experience
• There are still some spots available§ Sign up for a slot in CMS
• Will keep running after September 17§ Will open additional slots after the due date§ Will help students revise Assignment 1
9/7/17 Specifications & Testing 3
![Page 4: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/4.jpg)
Recall: The Python API
9/7/17 Specifications & Testing 4
Function name
Possible arguments
What the function evaluates toModule
![Page 5: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/5.jpg)
Recall: The Python API
9/7/17 Specifications & Testing 5
Function name
Possible arguments
What the function evaluates toModule
• This is a specification§ Enough info to use func.§ But not how to implement
• Write them as docstrings
![Page 6: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/6.jpg)
Anatomy of a Specification
def greet(n):"""Prints a greeting to the name n
Greeting has format 'Hello <n>!'Followed by conversation starter.
Parameter n: person to greetPrecondition: n is a string"""print('Hello '+n+'!')print('How are you?')
9/7/17 Specifications & Testing 6
One line description,followed by blank line
![Page 7: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/7.jpg)
Anatomy of a Specification
def greet(n):"""Prints a greeting to the name n
Greeting has format 'Hello <n>!'Followed by conversation starter.
Parameter n: person to greetPrecondition: n is a string"""print('Hello '+n+'!')print('How are you?')
9/7/17 Specifications & Testing 7
One line description,followed by blank line
More detail about the function. It may be many paragraphs.
![Page 8: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/8.jpg)
Anatomy of a Specification
def greet(n):"""Prints a greeting to the name n
Greeting has format 'Hello <n>!'Followed by conversation starter.
Parameter n: person to greetPrecondition: n is a string"""print('Hello '+n+'!')print('How are you?')
9/7/17 Specifications & Testing 8
One line description,followed by blank line
More detail about the function. It may be many paragraphs.
Parameter description
![Page 9: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/9.jpg)
Anatomy of a Specification
def greet(n):"""Prints a greeting to the name n
Greeting has format 'Hello <n>!'Followed by conversation starter.
Parameter n: person to greetPrecondition: n is a string"""print('Hello '+n+'!')print('How are you?')
9/7/17 Specifications & Testing 9
One line description,followed by blank line
More detail about the function. It may be many paragraphs.
Parameter description
Precondition specifies assumptions we make about the arguments
![Page 10: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/10.jpg)
One line description,followed by blank line
Anatomy of a Specification
def to_centigrade(x):"""Returns: x converted to centigrade
Value returned has type float.
Parameter x: temp in fahrenheitPrecondition: x is a float"""return 5*(x-32)/9.0
9/7/17 Specifications & Testing 10
More detail about the function. It may be many paragraphs.
Parameter description
Precondition specifies assumptions we make about the arguments
![Page 11: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/11.jpg)
One line description,followed by blank line
Anatomy of a Specification
def to_centigrade(x):"""Returns: x converted to centigrade
Value returned has type float.
Parameter x: temp in fahrenheitPrecondition: x is a float"""return 5*(x-32)/9.0
9/7/17 Specifications & Testing 11
“Returns” indicates afruitful function
More detail about the function. It may be many paragraphs.
Parameter description
Precondition specifies assumptions we make about the arguments
![Page 12: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/12.jpg)
Preconditions
• Precondition is a promise§ If precondition is true,
the function works§ If precondition is false,
no guarantees at all• Get software bugs when
§ Function precondition is not documented properly
§ Function is used in ways that violates precondition
>>> to_centigrade(32.0)0.0>>> to_centigrade(212)100.0
9/7/17 Specifications & Testing 12
![Page 13: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/13.jpg)
Preconditions
• Precondition is a promise§ If precondition is true,
the function works§ If precondition is false,
no guarantees at all• Get software bugs when
§ Function precondition is not documented properly
§ Function is used in ways that violates precondition
>>> to_centigrade(32.0)0.0>>> to_centigrade(212)100.0>>> to_centigrade('32')Traceback (most recent call last):File "<stdin>", line 1, in <module>File "temperature.py", line 19 …
TypeError: unsupported operand type(s) for -: 'str' and 'int'
9/7/17 Specifications & Testing 13
Precondition violated
![Page 14: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/14.jpg)
Test Cases: Finding Errors• Bug: Error in a program. (Always expect them!)• Debugging: Process of finding bugs and removing them. • Testing: Process of analyzing, running program, looking for bugs.• Test case: A set of input values, together with the expected output.
def number_vowels(w):"""Returns: number of vowels in word w.
Precondition: w string w/ at least one letter and only letters"""pass # nothing here yet!
9/7/17 Specifications & Testing 14
Get in the habit of writing test cases for a function from the function’s specification —even before writing the function’s body.
![Page 15: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/15.jpg)
Test Cases: Finding Errors• Bug: Error in a program. (Always expect them!)• Debugging: Process of finding bugs and removing them. • Testing: Process of analyzing, running program, looking for bugs.• Test case: A set of input values, together with the expected output.
def number_vowels(w):"""Returns: number of vowels in word w.
Precondition: w string w/ at least one letter and only letters"""pass # nothing here yet!
9/7/17 Specifications & Testing 15
Get in the habit of writing test cases for a function from the function’s specification —even before writing the function’s body.
Some Test Cases§ number_vowels('Bob')
Answer should be 1§ number_vowels('Aeiuo')
Answer should be 5§ number_vowels('Grrr')
Answer should be 0
![Page 16: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/16.jpg)
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
9/7/17 Specifications & Testing 16
Representative Tests fornumber_vowels(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
![Page 17: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/17.jpg)
How Many “Different” Tests Are Here?
INPUT OUTPUT'hat' 1'charm' 1'bet' 1'beet' 2'beetle' 3
9/7/17 Specifications & Testing 17
number_vowels(w)
A: 2B: 3C: 4D: 5E: I do not know
![Page 18: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/18.jpg)
How Many “Different” Tests Are Here?
INPUT OUTPUT'hat' 1'charm' 1'bet' 1'beet' 2'beetle' 3
9/7/17 Specifications & Testing 18
number_vowels(w)
A: 2B: 3C: 4D: 5E: I do not know
• If in doubt, just add more tests• You are never penalized for too many tests
CORRECT(ISH)
![Page 19: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/19.jpg)
Running Example
• The following function has a bug:def last_name_first(n):
"""Returns: copy of <n> but in the form <last-name>, <first-name>
Precondition: <n> is in the form <first-name> <last-name> with one or more blanks between the two names"""end_first = n.find(' ')first = n[:end_first]last = n[end_first+1:]return last+', '+first
• Representative Tests:§ last_name_first('Walker White') give 'White, Walker'§ last_name_first('Walker White') gives 'White, Walker'
9/7/17 Specifications & Testing 19
![Page 20: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/20.jpg)
Running Example
• The following function has a bug:def last_name_first(n):
"""Returns: copy of <n> but in the form <last-name>, <first-name>
Precondition: <n> is in the form <first-name> <last-name> with one or more blanks between the two names"""end_first = n.find(' ')first = n[:end_first]last = n[end_first+1:]return last+', '+first
• Representative Tests:§ last_name_first('Walker White') give 'White, Walker'§ last_name_first('Walker White') gives 'White, Walker'
9/7/17 Specifications & Testing 20
Look at preconditionwhen choosing tests
![Page 21: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/21.jpg)
Unit Test: A Special Kind of Script
• Right now to test a function we do the following§ Start the Python interactive shell§ Import the module with the function§ Call the function several times to see if it is okay
• But this is incredibly time consuming!§ Have to quit Python if we change module§ Have to retype everything each time
• What if we made a second Python module/script?§ This module/script tests the first one
9/7/17 Specifications & Testing 21
![Page 22: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/22.jpg)
Unit Test: A Special Kind of Script
• A unit test is a script that tests another module§ It imports the other module (so it can access it)§ It imports the cornell module (for testing)§ It defines one or more test cases
• A representative input• The expected output
• The test cases use the cornell function
def assert_equals(expected,received):"""Quit program if expected and received differ"""
9/7/17 Specifications & Testing 22
![Page 23: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/23.jpg)
Testing last_name_first(n)
import name # The module we want to testimport cornell # Includes the test procedures
# First test caseresult = name.last_name_first('Walker White')cornell.assert_equals('White, Walker', result)
# Second test caseresult = name.last_name_first('Walker White') cornell.assert_equals('White, Walker', result)
print('Module name is working correctly')9/7/17 Specifications & Testing 23
![Page 24: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/24.jpg)
Testing last_name_first(n)
import name # The module we want to testimport cornell # Includes the test procedures
# First test caseresult = name.last_name_first('Walker White')cornell.assert_equals('White, Walker', result)
# Second test caseresult = name.last_name_first('Walker White') cornell.assert_equals('White, Walker', result)
print('Module name is working correctly')9/7/17 Specifications & Testing 24
InputActual Output
Expected Output
![Page 25: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/25.jpg)
Testing last_name_first(n)
import name # The module we want to testimport cornell # Includes the test procedures
# First test caseresult = name.last_name_first('Walker White')cornell.assert_equals('White, Walker', result)
# Second test caseresult = name.last_name_first('Walker White') cornell.assert_equals('White, Walker', result)
print('Module name is working correctly')9/7/17 Specifications & Testing 25
Message will print out only if no errors.
Quits Pythonif not equal
![Page 26: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/26.jpg)
Using Test Procedures
• In the real world, we have a lot of test cases§ I wrote 20000+ test cases for a C++ game library§ You need a way to cleanly organize them
• Idea: Put test cases inside another procedure§ Each function tested gets its own procedure§ Procedure has test cases for that function§ Also some print statements (to verify tests work)
• Turn tests on/off by calling the test procedure9/7/17 Specifications & Testing 26
![Page 27: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/27.jpg)
Test Procedure
def test_last_name_first():"""Test procedure for last_name_first(n)""”print('Testing function last_name_first')result = name.last_name_first('Walker White')cornell.assert_equals('White, Walker', result)result = name.last_name_first('Walker White') cornell.assert_equals('White, Walker', result)
# Execution of the testing codetest_last_name_first()print('Module name is working correctly')
9/7/17 Specifications & Testing 27
![Page 28: Lecture 6 - Cornell University · Announcements For This Lecture Last Call • Acad. Integrity Quiz • Take it by tomorrow • Also remember survey Assignment 1 • Posted on web](https://reader034.fdocuments.in/reader034/viewer/2022042315/5f03cd7e7e708231d40ad889/html5/thumbnails/28.jpg)
Test Procedure
def test_last_name_first():"""Test procedure for last_name_first(n)""”print('Testing function last_name_first')result = name.last_name_first('Walker White')cornell.assert_equals('White, Walker', result)result = name.last_name_first('Walker White') cornell.assert_equals('White, Walker', result)
# Execution of the testing codetest_last_name_first()print('Module name is working correctly')
9/7/17 Specifications & Testing 28
No tests happen if you forget this