Homework 11: Design by contract & unit testing

Assigned
Wednesday October 26, 2016
Due
5 p.m., Friday October 28, 2016
Summary
The goal of this assignment is to gain further experience with pre-conditions, post-conditions, and unit tesitng.
Collaboration
Do this assignment alone or with a partner of your choice.
Submitting
Submit a Python program using the online turnin form. See below.
Scoring
10 points

Assignment

Create a module named unit_test.py. You will write all of your code in this module.

Your task is to document and test some functions, then build and test another function.

Part 1: find

Copy the find function from exercise 7.2.12. Write documentation for it, including preconditions and postconditions. Add assert statements to enforce those conditions.

Then, write a function named test_find that tests the given find function. Make sure to write tests for common cases, boundary cases, and edge cases.

Part 2: power

Here is documentation for a power function:
def power(base, exp):
'''
power calculates b to the power of exp without using
the math module or the ** operator, and returns the result.
This function adheres to the interpretation that
0 to the 0 power is 1.

Preconditions:
base is an integer
exp is a non-negative integer

Postconditions:
returns an integer that is the calculation of base to the exp power
'''

Write a function test_power that will test common cases, boundary cases, and edge cases.

Then, implement the power function, writing assert statements to enforce the given preconditions and postconditions. Make sure that you do not use ** or the math module.

Suggestion

If you want to test that an assertion fails when the function is given invalid input, use code similar to the following:

try:
power(1, -1)
except AssertionError:
print("Assertion fails as expected")

Grading and Submission

Please style your code per section 3.4 of the textbook.

Submit one file, unit_test.py, through the online turnin form.

  • find preconditions, postconditions, and tests - 5 pts

  • power implementation and tests - 5 pts



Janet Davis (davisj@whitman.edu).
This assignment is adapted from our textbook.

Created October 17, 2016
Last revisedOctober 26, 2016, 01:38:16 PM PDT
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.