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


--> report_file open and path is an external collaborative (platform and system environment dependent) which is handled in the function. hence, it is untestable.


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



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

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


class FizzBuzz(object):
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")
file_handle.write(msg + "\n")



report_file.close()

if "__main__" == __name__:
fb = FizzBuzz()
fb.report(range(100))
fb = FizzBuzz()
file_handle = open('temp.txt', 'w') # can create open wrapper
fb.report(range(100), file_handle)
file_handle.close()


45 changes: 42 additions & 3 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,30 @@
import FizzBuzz
"""
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]
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 +40,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 All @@ -37,4 +76,4 @@ def test_report(self):


if __name__ == "__main__":
unittest.main()
unittest.main()
55 changes: 53 additions & 2 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 @@ -13,7 +25,46 @@


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]
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 Expand Up @@ -56,4 +107,4 @@ def test_report_for_empty_list(self):
pass

if __name__ == "__main__":
unittest.main()
unittest.main()