$30
HW2: Improving Coverage
Points 21
Introduction
Last week, you applied black box testing techniques. This meant that you had no access
to the source and had to write your tests based solely on the specification. This week, you
will apply white box testing techniques. Therefore, I am providing you with the code you will be testing.
Also, since white box testing doesn't care about the specification, the function under test is purely
contrived and serves no purpose! Well, that isn't entirely true, the purpose is to give you an opportunity
to flex your coverage muscles, but it doesn't do anything.
Course Learning Outcome(s):
Apply testing techniques, including black-box and white-box techniques, automatic testing
activities, and regression testing (CLO 4)
Module Learning Outcome(s):
Apply white box testing techniques
Description
For this assignment, you will have to achieve 100% Branch and Condition Coverage. For this
assignment, this means that for each conditional statement, you need to have a test case for each
combination of conditions. So if your conditional statement was a or b you would need to have a test
case for each row of the following truth table:
Truth table for "a or b"
a b outcome
T T T
T F T
F T T
F F F
If you recall what I said in the exploration, Branch and Condition coverage is usually not done due to
the number of tests required. We are doing it here because we are only testing a single function, not
an entire program. The function under test is defined below.
Behold! The Source!
import math
def contrived_func(val):
a = val - math.sqrt(abs(val*2)) > 5
b = val ** 2 % 2 == 0
c = val * 5 < 100
d = -val ** 3 > 0
if a or b:
if (a and b) or (b and c) and d:
pass
else:
pass
else:
if (a or b) or (b or c):
pass
else:
pass
if a and b:
pass
else:
pass
Again, this function's only purpose is as a learning aid; it doesn't do anything. DO NOT try to
rationalize its behavior! This function is contained within contrived_func.py , so make sure you are
importing the correct file.
You need to write a series of unit tests that attempt to meet 100% Branch and Condition Coverage.
Once submitted to Gradescope, the autograder will run the tests against a modified version of the
above code. The contrived_func on Gradescope is functionally equivalent but includes print
statements when each condition combination is triggered. There are 18 such triggers, labeled C* (with
* representing a number). You will only receive full credit for the autograded portion if your test suite
triggers all 18 print statements.
Outside of the autograded portion, your test suite will also be graded based on how many tests were
required to uncover all 18 triggers. Full points will only be rewarded if you can do it with 8 or fewer test
cases. Please note that if your tests do not pass the autograder with full marks, you will not be eligible
for these Efficient Testing points. Also, for the purposes of the efficiency points, each assert/call to
contrived_func counts as a single test, so there is no putting in multiple asserts in each test case.
Finally, as was the case last week, your test suite needs to be free of linting errors.
Hints
Truth Tables
You should immediately notice from GradeScope that the names of the conditions we are trying to
trigger are not sequential. For example, there are C3 and C4 , but no C1 or C2 . This is because they
are named after given rows from the truth tables I generated when crafting this assignment. You will
HW2 Rubric
find that some rows are not possible to verify every conditional in every permutation ( T or F will never
evaluate the F due to short circuit evaluation). You can only write a test for rows that are possible.
Therefore, I highly recommend you write out, by hand, the truth tables for each conditional statement.
You should also ask yourself the question, how does having a nested if affect the truth tables? Feel
free to discuss this topic on Ed, but do not share your actual tables themselves.
Misc
You will need to include
if __name__ == '__main__':
unittest.main()
Feel free to take the provided source above and create your own contrived_func.py to run your
tests against to help find errors with your testing file
Do not use Random Testing for this assignment, you will get your chance in HW3: Random
Testing Hands On (https://canvas.oregonstate.edu/courses/1923080/assignments/9307830)
What to turn in
Submit to Gradescope your testing suite; it must be named tests.py
This file must be free of PEP8 linting errors
Resources
PyCharm Coverage Tools (https://www.jetbrains.com/help/pycharm/configuring-code-coveragemeasurement.html)
Truth Table Generator (https://web.stanford.edu/class/cs103/tools/truth-table-tool/)
Criteria Ratings Pts 1 pts 1 pts 1 pts 1 pts 1 pts 1 pts 1 pts 1 pts 1 pts 1 pts 1 pts
C
1
1
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
1
3
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
1
5
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
1
8
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
0
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
1
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
2
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
3
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
4
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
5
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
C
2
6
F
o
u
n
d
1
p
t
s
F
u
l
l
M
a
r
k
s
0
p
t
s
N
o
M
a
r
k
s
Total Points: 21
Criteria Ratings Pts
1 pts
1 pts
1 pts
1 pts
1 pts
1 pts
1 pts
2 pts
1 pts
C27 Found 1 pts
Full Marks
0 pts
No Marks
C28 Found 1 pts
Full Marks
0 pts
No Marks
C3 Found 1 pts
Full Marks
0 pts
No Marks
C4 Found 1 pts
Full Marks
0 pts
No Marks
C7 Found 1 pts
Full Marks
0 pts
No Marks
C8 Found 1 pts
Full Marks
0 pts
No Marks
C9 Found 1 pts
Full Marks
0 pts
No Marks
Testing Efficiency 2 pts
Full Marks
Used 8 or fewer
tests
1 pts
Half Marks
Used 10 or fewer
tests
0 pts
No Marks
Used more than 10 tests or didn't
trigger all the bugs
test.py is free of linting
errors
1 pts
Full Marks
There are no linting
errors
0.5 pts
Half Marks
There are no more than 2
linting errors
0 pts
No Marks
There are 3 or more
linting errors