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
12 changes: 8 additions & 4 deletions FizzBuzz.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"""
Q1. Why is the report method untestable ? [2 pts]


report_file takes pointer of file , available on local machine- external collaborators


Q2. How will you change the api of the report method to make it more testable ? [2 pts]


Passing a Filehandlewrapper to report function , so that function should not pick his collaborators.
We can use -
a. Filehandlewrapper - a wrapper class defined to have interface function for OPen
b. fileopener = using argument in report function, Implemented below

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

report_file = open('c:/temp/fizzbuzz_report.txt', 'w')
#report_file = Filehandlewrapper.open
report_file = fileopener('c:/temp/fizzbuzz_report.txt', 'w')

for number in numbers:
msg = str(number) + " "
Expand Down
17 changes: 16 additions & 1 deletion TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ def tearDown(self):
self.fb = None

def test_report(self):
pass
#mock the file handle
mock_opener_interface = self.mock()
mock_file = self.mock()
self.expectAndReturn(mock_opener_interface.open('c:/temp/fizzbuzz_report.txt', 'w'), mock_file)
mock_file.write("3 fizz \n")
mock_file.close()

#replay
self.replay()

#call API
self.fb.report(numbers, fileOpener=mock_opener_interface.open)

# verify
self.verify()




Expand Down
27 changes: 22 additions & 5 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import FizzBuzz
"""
Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts]
#class setup - at start
setUpClass FizzBuzzStubbed





#test setup before each test
setup
test_report
teardown
#test setup before each test
.setup
test_report
teardown
#class teardown - at end
.tearDownClass



Expand All @@ -16,8 +24,17 @@
report method of FizzBuzz object from a test case. [3 pts]

"""

class MyStub(object):
pass

def __init__(self):
self.values = []

def write(self, value):
self.values.append(value)

def close(self):
self.closed = True



Expand Down