Question: This question concerns devising a set of tests for a Python function that cumulatively achieve statement coverage. A python module called timeutil.py was provided. The
This question concerns devising a set of tests for a Python function that cumulatively achieve statement coverage.
A python module called timeutil.py was provided. The module contains a function called validate. The purpose of this function is to accept a string value as a parameter and determine whether it is a valid representation of a 12 hour clock reading.
The string is a valid representation if the following applies:
- It comprises 1 or 2 leading digits followed by a colon followed by 2 digits followed by a space followed by a two letter suffix.
- The leading digit(s) form an integer value in the range 1 to 12.
- The 2 digits after the colon form an integer value in the range 0 to 59.
- The suffix is am or pm.
Leading and trailing whitespace is ignored.
Examples of valid strings:
01:10 am
1:15 pm
12:59 am
11:01 pm
The task:
1. Develop a set of 5 test cases that achieve statement coverage.
2. Code your tests as a doctest script suitable for execution within Wing IDE (like the testchecker.py module described in the appendix).
3. Save your doctest script as testtimeutil.py.
NOTE: make sure the docstring in your script contains a blank line before the closing . (The marker requires it.)
NOTE: the validate function is believed to be error free.
----------------------------------------------------------------------------------------------------------------------------------------
Below is the program required for answering the question and it is also for reference:
import trace
def validate(time): time=time.strip() colon = time.find(':') if colon<1: return false else: hours = time[:colon] if len(hours)>2 or len(hours)<1 or not hours.isdigit(): return False suffix = time[-3:] if not suffix==' am' and not suffix==' pm': return False minutes = time[colon+1:len(time)-3] if not len(minutes)==2 or not minutes.isdigit(): return False
hours = int(hours) minutes = int(minutes) return hours>0 and hours<13 and minutes>-1 and minutes<60
------------------------------------------------------------------------------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
