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

Ans:- The report method has external depndencies.
It depends on the "open function" and the Txt file.



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


Ans:- The API of the report method can be changed by the the number of arguments it accepts as
"report(self,numbers,filehandler)"
where "filehandler" will be used for creating an impression of reading values from file.

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

report_file = open('c:/temp/fizzbuzz_report.txt', 'w')
report_file = opener('c:/temp/fizzbuzz_report.txt', 'w')

for number in numbers:
msg = str(number) + " "
Expand Down
42 changes: 41 additions & 1 deletion TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@
"""
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]

Ans:-

def test_report(self):
#mock the file handle
mock_opener = self.mock()
mock_file = self.mock()
self.expectAndReturn(mock_opener.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, opener=mock_opener.open)

# verify
self.verify()





"""
class TestFizzBuzzMocked(pymock.PyMockTestCase):

Expand All @@ -17,7 +41,23 @@ def tearDown(self):
self.fb = None

def test_report(self):
pass
#mock the file handle
mock_opener = self.mock()
mock_file = self.mock()
self.expectAndReturn(mock_opener.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, opener=mock_opener.open)

# verify
self.verify()





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

Ans:-

setUpClass FizzBuzzStubbed
setup
test_report
teardown
test_report
setup
test_report
teardown
tearDownClass




Expand All @@ -14,6 +26,50 @@

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]

Ans:-

###############myStub Class############################
class MyStub(object):

def gen_open_stub(output_stub):
def open(fpath, mode):
return output_stub
return open

def __init__(self):
self.values = []
self.cnt = 0
def write(self, value):
self.values.append(value)

def close(self):
self.closed = True

def readline(self):
if self.cnt <= len(self.values):
ret_val = self.values[self.cnt]
self.cnt += 1
return ret_val
else:
raise StopError()

############# And appropriate changes required for test_report method ##########################


def test_report(self):
print "test_report"
output_stub = MyStub()

my_open= MyStub.gen_open_stub(output_stub)
fb = FizzBuzz.FizzBuzz()
fb.report([33], my_open)
self.assertEqual(output_stub.readline(), '33 fizz \n' )
fb.report([55], my_open)
self.assertEqual(output_stub.readline(), '55 buzz \n' )
fb.report([15], my_open)
self.assertEqual(output_stub.readline(), '15 fizz buzz \n' )
pass

"""
class MyStub(object):
Expand Down