specifications testing
play

Specifications & Testing [Andersen, Gries, Lee, Marschner, Van - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 6 Specifications & Testing [Andersen, Gries, Lee, Marschner, Van Loan, White] Recall: The Python API Function name Number of arguments What the function evaluates to 2/14/17


  1. CS 1110: Introduction to Computing Using Python Lecture 6 Specifications & Testing [Andersen, Gries, Lee, Marschner, Van Loan, White]

  2. Recall: The Python API Function name Number of arguments What the function evaluates to 2/14/17 Specifications & Testing 2

  3. Recall: The Python API Function name Number of arguments What the function evaluates to • This is a specification  Enough info to use func.  But not how to implement • Write them as docstrings 2/14/17 Specifications & Testing 3

  4. Anatomy of a Specification One line description, def greet(n): followed by blank line """Prints a greeting to the name n More detail about the Greeting has format 'Hello <n>!' function. It may be Followed by conversation starter. many paragraphs. Parameter n: person to greet Parameter description Precondition: n is a string""" print 'Hello '+n+'!’ Precondition specifies print 'How are you?' assumptions we make about the arguments 2/14/17 Specifications & Testing 4

  5. Anatomy of a Specification One line description, def to_centigrade(x): followed by blank line """Returns: x converted to centigrade More detail about the Value returned has type float. function. It may be many paragraphs. Parameter x: temp in Fahrenheit Parameter description Precondition: x is a float""" return 5*(x-32)/9.0 Precondition specifies assumptions we make about the arguments 2/14/17 Specifications & Testing 5

  6. Anatomy of a Specification “Returns” indicates a def to_centigrade(x): fruitful function """Returns: x converted to centigrade More detail about the Value returned has type float. function. It may be many paragraphs. Parameter x: temp in Fahrenheit Parameter description Precondition: x is a float""" return 5*(x-32)/9.0 Precondition specifies assumptions we make about the arguments 2/14/17 Specifications & Testing 6

  7. Preconditions >>> to_centigrade(32.0) • Precondition is a promise  If precondition is true, 0.0 the function works >>> to_centigrade(212)  If precondition is false, 100.0 no guarantees at all >>> to_centigrade('32') • Get software bugs when Traceback (most recent call last):  Function precondition is File "<stdin>", line 1, in <module> not documented properly File "temperature.py", line 19 …  Function is used in ways TypeError: unsupported operand that violates precondition type(s) for -: 'str' and 'int' Precondition violated 2/14/17 Specifications & Testing 7

  8. NASA Mars Climate Orbiter Source: Mars Climate Orbiter Mishap Investigation Board Source: NASA Phase I Report 2/14/17 Specifications & Testing 8

  9. 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. Get in the habit of writing test cases for a function from the function’s specification – even before writing the function’s body. 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! 2/14/17 Specifications & Testing 9

  10. Test Cases: Finding Errors • Bug : Error in a program. (Always expect them!) Some Test Cases Debugging : Process of finding bugs and removing them. •  number_vowels('Bob') Answer should be 1 • Testing : Process of analyzing, running program, looking for bugs.  number_vowels('Aeiuo') • Test case : A set of input values, together with the expected output. Answer should be 5 Get in the habit of writing test cases for a function from the  number_vowels('Grrr') function’s specification – even before writing the function’s body. Answer should be 0 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! 2/14/17 Specifications & Testing 10

  11. Test Cases: Finding Errors • Bug : Error in a program. (Always expect them!) Some Test Cases Some Test Cases Debugging : Process of finding bugs and removing them. •   number_vowels('y') number_vowels('Bob') Answer should be 0? 1? Answer should be 1 • Testing : Process of analyzing, running program, looking for bugs.   number_vowels('Bobo') number_vowels('Aeiuo') • Test case : A set of input values, together with the expected output. Answer should be 1? 2? Answer should be 5 Get in the habit of writing test cases for a function from the  number_vowels('Grrr') function’s specification – even before writing the function’s body. Answer should be 0 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! 2/14/17 Specifications & Testing 11

  12. Representative Tests • Cannot test all inputs Representative Tests for  “Infinite” possibilities number_vowels(w) • Limit ourselves to tests • Word with just one vowel that are representative  For each possible vowel!  Each test is a significantly different input • Word with multiple vowels  Every possible input is  Of the same vowel similar to one chosen  Of different vowels • An art, not a science • Word with only vowels  If easy, never have bugs • Word with no vowels  Learn with much practice 2/14/17 Specifications & Testing 12

  13. 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:] Look at precondition return last+', '+first when choosing tests • Representative Tests:  last_name_first('Erik Andersen') gives 'Andersen, Erik'  last_name_first(‘Erik Andersen') gives 'Andersen, Erik' 2/14/17 Specifications & Testing 13

  14. cornelltest module • Contains useful testing functions • Need to download it and put in same folder as other files • Available at: http://www.cs.cornell.edu/courses/cs1110/2017sp/lectures/02-14-17/modules/cornelltest.py 2/14/17 Specifications & Testing 14

  15. 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 cornelltest module (for testing)  It defines one or more test cases that each include: • A representative input • The expected output • The test cases use the cornelltest function def assert_equals(expected,received): """Quit program if expected and received differ""" 2/14/17 Specifications & Testing 15

  16. Testing last_name_first(n) import name # The module we want to test import cornelltest # Includes the test procedures # First test case result = name.last_name_first('Erik Andersen') cornelltest.assert_equals('Andersen, Erik', result) # Second test case result = name.last_name_first('Erik Andersen') cornelltest.assert_equals('Andersen, Erik', result) print 'Module name is working correctly' 2/14/17 Specifications & Testing 16

  17. Testing last_name_first(n) import name # The module we want to test import cornelltest # Includes the test procedures Actual Output Input # First test case result = name.last_name_first('Erik Andersen') cornelltest.assert_equals('Andersen, Erik', result) Expected Output # Second test case result = name.last_name_first('Erik Andersen') cornelltest.assert_equals('Andersen, Erik', result) print 'Module name is working correctly' 2/14/17 Specifications & Testing 17

  18. Testing last_name_first(n) import name # The module we want to test import cornelltest # Includes the test procedures # First test case result = name.last_name_first('Erik Andersen') Quits Python cornelltest.assert_equals('Andersen, Erik', result) if not equal # Second test case result = name.last_name_first('Erik Andersen') cornelltest.assert_equals('Andersen, Erik', result) Message will print print 'Module name is working correctly' out only if no errors. 2/14/17 Specifications & Testing 18

  19. Using Test Procedures • In the real world, we have a lot of test cases  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 procedure 2/14/17 Specifications & Testing 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend