From f52d255956344aae3ff0790d421f34cec810df11 Mon Sep 17 00:00:00 2001 From: shubhashishtiwari Date: Wed, 27 Jun 2012 14:33:30 +0530 Subject: [PATCH 1/3] Answered the questions --- FizzBuzz.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..8fb496d 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -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): From 8a27f3d6f17d916ca32ca98931c6a8e80b35c8a2 Mon Sep 17 00:00:00 2001 From: shubhashishtiwari Date: Wed, 27 Jun 2012 14:38:42 +0530 Subject: [PATCH 2/3] Implemented mystub class for the external contributors --- TestFizzBuzzStubbed.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..881d226 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,10 +4,15 @@ """ 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 @@ -15,6 +20,16 @@ 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 From ec2bf8260bcd023ed0d830f080f2a623364e590e Mon Sep 17 00:00:00 2001 From: shubhashishtiwari Date: Wed, 27 Jun 2012 14:42:41 +0530 Subject: [PATCH 3/3] Update master --- TestFizzBuzzMocked.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..fdd575f 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -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):