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
43 changes: 43 additions & 0 deletions FizzBuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,55 @@
Q1. Why is the report method untestable ? [2 pts]


1)open command is an external collaborator, which should be passed as an argument to the report method
2)File handler to be created for the write and close method


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

The defintion of report will accept three arguments report_file(self, numbers,fileWrapper)
the calls to open , write and close will be substituted by the fileWrapper.
This wrapper can be mocked and code logic can be tested accordingly.


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

filehandle.open()
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_numbersfound = True

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

filehandle.close()

if "__main__" == __name__:
fb = FizzBuzz()
wrapper = filewrapper('fizzbuzz_report.txt', 'w')
fb.report(range(100),wrapper)

class filewrapper:
def __init__(self,fname,mode):
self.fname = fname
self.mode = mode
def open(self):
self.fh = open(self.fname,self.mode)
return self.fh

def write(self,msg):
self.fh.write(msg)

def close(self):
self.fh.close()

"""
class FizzBuzz(object):
def report(self, numbers):
Expand Down
12 changes: 12 additions & 0 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
"""
Q5. Write the psuedocode for the test_repport method, such that it uses PyMock
mock objects to test the report method of FizzBuzz. [5 pts]

def test_report(self):
mockFile = self.mock()
mockFileWrapper = self.mockFile('fizzbuzz_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()

"""
class TestFizzBuzzMocked(pymock.PyMockTestCase):

Expand Down
23 changes: 19 additions & 4 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@
"""
Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts]





setUpClass FizzBuzzStubbed
setup
test_report
teardown
test_report
setup
test_report
teardown
tearDownClass




Q4. Implement MyStub class so that you can send it as a fake object to the
report method of FizzBuzz object from a test case. [3 pts]

class MyStub(object):
def __init__(self):
self.openflag=False
self.values=[]
def open(self):
self.openflag = True
def write(self,msg):
self.values.append(msg)
def close():
self.openflag = False # close fake "file"
"""
class MyStub(object):
pass
Expand Down