diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..77186a3 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,18 +1,18 @@ """ Q1. Why is the report method untestable ? [2 pts] - - - +Ans: +- report() method is instantiating collaborators like write,close,etc. +- have external dependency. Q2. How will you change the api of the report method to make it more testable ? [2 pts] - - +Ans: + def report(self, numbers,fileHandler): """ class FizzBuzz(object): - def report(self, numbers): + def report(self, numbers,fileHandler=open): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + report_file = fileHandler('c:/temp/fizzbuzz_report.txt', 'w') for number in numbers: msg = str(number) + " " @@ -31,6 +31,4 @@ def report(self, numbers): if "__main__" == __name__: fb = FizzBuzz() - fb.report(range(100)) - - + fb.report(range(100)) \ No newline at end of file diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..e81ebee 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -1,6 +1,7 @@ import unittest import pymock import FizzBuzz +import TestFizzBuzzStubbed """ 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] @@ -16,25 +17,20 @@ def tearDown(self): super(TestFizzBuzzMocked, self).tearDown() self.fb = None - def test_report(self): - pass - - - - - - - - - - - - - - - - - + def test_report(self): + #Create mock + fileHandlerWrapperMock = self.mock() + #Set expectations + self.expectAndReturn(fileHandlerWrapperMock.open('c:/temp/fizzbuzz_report.txt'),'w') + numbers = ['1','2','3','4'] + fileHandlerWrapperMock.write(numbers) + fileHandlerWrapperMock.close() + #Replay + self.replay() + #call api + self.fb.record(numbers,fileHandlerWrapperMock) + #verify + self.verify() if __name__ == "__main__": unittest.main() diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..d395b90 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -3,30 +3,41 @@ import FizzBuzz """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] +Ans: +setUpClass FizzBuzzStubbed +setup +test_report +teardown +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] +Ans: """ class MyStub(object): - pass - - - - - - + def __init__(self): + pass + + def write(self, number): + self.numbers.append(number) + + def close(self): + self.closed = True + def gen_open_stub(my_stub): + def open(fpath, mode): + return my_stub + return open - class TestFizzBuzzStubbed(unittest.TestCase): @classmethod @@ -49,11 +60,19 @@ def tearDown(self): def test_report(self): print "test_report" - pass - + numbers = ['1','2','3','4'] + my_stub = MyStub() + filehandler = my_stub.gen_open_stub(my_stub) + self.fb.report(numbers,filehandler) + self.assertEqual(numbers[0], '3 fizz \n') + def test_report_for_empty_list(self): - print "test_report" - pass + print "test_report" + numbers = [] + my_stub = MyStub() + filehandler = my_stub.gen_open_stub(my_stub) + self.fb.report(numbers,filehandler) + if __name__ == "__main__": unittest.main()