7. Testing
7. Testing
CSC1015F Assignment 7
Testing
Assignment Instructions
This assignment involves constructing tests, assembling automated ‘doctest’ test scripts
for statement and path coverage, and constructing Python programs that manipulate lists,
dictionaries and strings using arrays. You can use the following built-in functions in Python:
map( ), math.sqrt( ), split( ).
READ the APPENDIX on pages 4 to 7 before attempting the questions in this Assignment.
NOTE Your solutions to this assignment will be evaluated for correctness and for the following
qualities:
• Documentation
o Use of comments at the top of your code to identify program purpose,
author, and date.
o Use of comments within your code to explain each non-obvious functional
unit of code.
• General style/readability
o The use of meaningful names for variables and functions.
• Algorithmic qualities
o Efficiency, simplicity
These criteria will be manually assessed by a tutor and commented upon. In this assignments, up to
10 marks will be deducted for deficiencies.
NOTE: make sure the docstring in your script contains a blank line before the closing “””.
(The automarker requires it.)
NOTE: the aswords() function is believed to be error free.
Page 1 of 7
Version 02/04/2024 13:45
Examples of palindromes are (note that white spaces are ignored – see the last examples):
989
a
aba
racecar
a santa at nasa
Examples of strings that are not palindromes (note case sensitivity – last two examples):
34563
ab
Racecar
A Santa at NASA
Your task:
4. Develop a set of 5 test cases that achieve statement coverage.
5. Code your tests as a doctest script suitable for execution within Wing IDE (like the
testchecker.py module described in the appendix).
6. Save your doctest script as ‘testpalindrome.py’.
NOTE: make sure the docstring in your script contains a blank line before the closing “””.
(The automarker requires it.)
NOTE: the palindrome() function is believed to be error free.
Page 2 of 7
Version 02/04/2024 13:45
NOTE: make sure the docstring in your script contains a blank line before the closing “””.
(The automarker requires it.)
NOTE: the validate() function is believed to be error free.
Submission
Create and submit a Zip file called ‘ABCXYZ123.zip’ (where ABCXYZ123 is YOUR student
number) containing testnumberutil.py, testpalindrome.py, and
testtimeutil.py.
Page 3 of 7
Version 02/04/2024 13:45
3 3, 4, 5, 7, 8, 9 (30, 20) 2
4 3, 4, 5, 7, 9 (30, 30) 0
Note that we analyse the code to identify paths and select inputs that will cause that path to
be followed. Given the inputs for a path, we study the function specification (the description
of its intended behaviour) to determine the expected output.
Page 4 of 7
Version 02/04/2024 13:45
# testchecker.py
"""
>>> import checker
>>> checker.check(20, 20)
3
>>> checker.check(20, 30)
1
>>> checker.check(30, 20)
2
>>> checker.check(30, 30)
0
"""
import doctest
doctest.testmod(verbose=True)
The script is enclosed within a Python docstring. The docstring begins with three double
quotation marks and ends with three double quotation marks.
NOTE: the blank line before the closing quotation marks is essential.
Following the docstring is an instruction to import the doctest module, followed by an
instruction to run the 'testmod()' function. (The parameter 'verbose=True' ensures
that the function prints what it's doing.)
If we save this as say, 'testchecker.py', and run it, here's the result:
Trying:
import checker
Expecting nothing
ok
Trying:
checker.check(20, 20)
Expecting:
3
**************************************************************
********
File "testchecker.py", line 3, in __main__
Failed example:
checker.check(20, 20)
Expected:
3
Got:
5
Trying:
checker.check(20, 30)
Expecting:
1
**************************************************************
********
File "testchecker.py", line 5, in __main__
Page 5 of 7
Version 02/04/2024 13:45
Failed example:
checker.check(20, 30)
Expected:
1
Got:
3
Trying:
checker.check(30, 20)
Expecting:
2
ok
Trying:
checker.check(30, 30)
Expecting:
0
ok
**************************************************************
********
1 items had failures:
2 of 5 in __main__
5 tests in 1 items.
3 passed and 2 failed.
***Test Failed*** 2 failures.
As might be expected, we have two failures because of the bug at line 6.
What happens is that doctest.testmod() locates the docstring, and looks for lines
within it that begin with '>>>'. Each that it finds, it executes. At each step it states what it is
executing and what it expects the outcome to be. If all is well, ok, otherwise it reports on
the failure.
The last section contains a summary of events.
If we correct the bug at line 6 in the check function and run the test script again, we get the
following:
Trying:
import checker
Expecting nothing
ok
Trying:
checker.check(20, 20)
Expecting:
3
ok
Trying:
checker.check(20, 30)
Expecting:
1
ok
Trying:
checker.check(30, 20)
Expecting:
2
ok
Page 6 of 7
Version 02/04/2024 13:45
Trying:
checker.check(30, 30)
Expecting:
0
ok
1 items passed all tests:
5 tests in __main__
5 tests in 1 items.
5 passed and 0 failed.
Test passed.
APPENDIX ENDS
Page 7 of 7