Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions FizzBuzz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Q1. Why is the report method untestable ? [2 pts]

.........
report_file open and path is external collaborative (platform and system environment dependent) which is handled in the function so it is untestable



Expand All @@ -10,27 +11,46 @@

"""
class FizzBuzz(object):
def report(self, numbers):

report_file = open('c:/temp/fizzbuzz_report.txt', 'w')
def report(self, numbers, file_handle):

for number in numbers:

msg = str(number) + " "

fizzbuzz_found = False

if number % 3 == 0:

msg += "fizz "

fizzbuzz_found = True

if number % 5 == 0:
msg += "buzz "
fizzbuzz_found = True

msg += "buzz "

fizzbuzz_found = True

if fizzbuzz_found:
report_file.write(msg + "\n")

report_file.close()
file_handle.write(msg + "\n")

if "__main__" == __name__:
fb = FizzBuzz()
fb.report(range(100))








if "__main__" == __name__:

fb = FizzBuzz()

file_handle = open('temp.txt', 'w') # can create open wrapper

fb.report(range(100), file_handle)

file_handle.close()

12 changes: 10 additions & 2 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ def tearDown(self):
self.fb = None

def test_report(self):
pass

#create mock
mockFileProvider = self.mock()
mockFileWrapper = self.mockFileProvider('report.txt', 'w')
#replay
self.replay()
#Call the report method with mocked filewrapper
numbers=range(100)
FizzBuzz.report(self.numbers,self.mockFileWrapper)
#verify
self.verify()



Expand Down
36 changes: 22 additions & 14 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import FizzBuzz
"""
Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts]
..............






setUpClass FizzBuzzStubbed
setup
test_report
teardown
.setup
test_report
teardown
.tearDownClass



Expand All @@ -17,16 +21,20 @@

"""
class MyStub(object):
pass







def __init__(self, values)
self.flag = False
self.values = []

def open(self)
self.flag = True

def write(self,msg)
self.values.append(msg)

def close(self)
self.flag = False


class TestFizzBuzzStubbed(unittest.TestCase):

@classmethod
Expand Down