specifications testing announcements for this lecture
play

Specifications & Testing Announcements For This Lecture Last - PowerPoint PPT Presentation

Lecture 6 Specifications & Testing Announcements For This Lecture Last Call Assignment 1 Acad. Integrity Quiz Posted on web page Take it by tomorrow Due Wed, Sep. 19 th Todays lab will help Also remember survey


  1. Lecture 6 Specifications & Testing

  2. Announcements For This Lecture Last Call Assignment 1 • Acad. Integrity Quiz • Posted on web page • Take it by tomorrow § Due Wed, Sep. 19 th § Today’s lab will help • Also remember survey § Revise until correct • Can work in pairs § One submission for pair § Mixer is TODAY 5-6 pm § 3rd Floor Lounge of Gates 9/11/18 Specifications & Testing 2

  3. One-on-One Sessions • Started Sunday: 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 19 § Will open additional slots after the due date § Will help students revise Assignment 1 9/11/18 Specifications & Testing 3

  4. Recall: The Python API Function name Possible arguments Module What the function evaluates to 9/11/18 Specifications & Testing 4

  5. Recall: The Python API Function name Possible arguments Module What the function evaluates to • This is a specification § Enough info to use func. § But not how to implement • Write them as docstrings 9/11/18 Specifications & Testing 5

  6. Anatomy of a Specification One line description, def greet(n): followed by blank line """Prints a greeting to the name n Greeting has format 'Hello <n>!' Followed by conversation starter. Parameter n: person to greet Precondition: n is a string""" print('Hello '+n+'!') print('How are you?') 9/11/18 Specifications & Testing 6

  7. 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 many paragraphs. Followed by conversation starter. Parameter n: person to greet Precondition: n is a string""" print('Hello '+n+'!') print('How are you?') 9/11/18 Specifications & Testing 7

  8. 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 many paragraphs. Followed by conversation starter. Parameter description Parameter n: person to greet Precondition: n is a string""" print('Hello '+n+'!') print('How are you?') 9/11/18 Specifications & Testing 8

  9. 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 many paragraphs. Followed by conversation starter. Parameter description Parameter n: person to greet Precondition: n is a string""" Precondition specifies print('Hello '+n+'!') assumptions we make about the arguments print('How are you?') 9/11/18 Specifications & Testing 9

  10. 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 9/11/18 Specifications & Testing 10

  11. Anatomy of a Specification One line description, “Returns” indicates a def to_centigrade(x): followed by blank line 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 9/11/18 Specifications & Testing 11

  12. 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 • Get software bugs when § Function precondition is not documented properly § Function is used in ways that violates precondition 9/11/18 Specifications & Testing 12

  13. 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 type(s) that violates precondition for -: 'str' and 'int' Precondition violated 9/11/18 Specifications & Testing 13

  14. 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! 9/11/18 Specifications & Testing 14

  15. 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! 9/11/18 Specifications & Testing 15

  16. Representative Tests • Cannot test all inputs Representative Tests for number_vowels(w) § “Infinite” possibilities • 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 9/11/18 Specifications & Testing 16

  17. How Many “Different” Tests Are Here? number_vowels(w) INPUT OUTPUT 'hat' A: 2 1 B: 3 'charm' 1 C: 4 'bet' 1 D: 5 'beet' 2 E: I do not know 'beetle' 3 9/11/18 Specifications & Testing 17

  18. How Many “Different” Tests Are Here? number_vowels(w) INPUT OUTPUT 'hat' A: 2 1 B: 3 CORRECT(ISH) 'charm' 1 C: 4 'bet' 1 D: 5 'beet' 2 E: I do not know 'beetle' 3 • If in doubt, just add more tests • You are never penalized for too many tests 9/11/18 Specifications & Testing 18

  19. 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/11/18 Specifications & Testing 19

  20. 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('Walker White') give 'White, Walker' § last_name_first('Walker White') gives 'White, Walker' 9/11/18 Specifications & Testing 20

  21. 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/11/18 Specifications & Testing 21

Recommend


More recommend