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
44 changes: 43 additions & 1 deletion FizzBuzz.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
"""
Q1. Why is the report method untestable ? [2 pts]


The report method is using the open command which is an external collaborator, the external collaborator should
be passed as an argument to the report 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
14 changes: 14 additions & 0 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
"""
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):
#mock file wrapper
mockFileProvider = self.mock()
mockFileWrapper = self.mockFileProvider('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
25 changes: 21 additions & 4 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@
"""
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]

external contributer for file operations needs to be faked in the MyStub class, this includes faking open,write and close operations

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