From 3b81b32547494336395195c38cf6180a8cca192b Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 17:52:11 +0530 Subject: [PATCH 1/7] Update master --- TestFizzBuzzStubbed.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..8fbfa42 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,12 +4,16 @@ """ 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 @@ -19,14 +23,7 @@ class MyStub(object): pass - - - - - - - - + class TestFizzBuzzStubbed(unittest.TestCase): @classmethod From 1d82a7b203a491d0d871f6fa01ca65590d28cd93 Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 18:21:12 +0530 Subject: [PATCH 2/7] Update master --- TestFizzBuzzMocked.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..633177f 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() + 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() From a036253769bb92a5092707da2c1472eaaa168a2b Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 18:42:07 +0530 Subject: [PATCH 3/7] Update master --- TestFizzBuzzMocked.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 633177f..e81ebee 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -21,7 +21,7 @@ def test_report(self): #Create mock fileHandlerWrapperMock = self.mock() #Set expectations - self.expectAndReturn() + self.expectAndReturn(fileHandlerWrapperMock.open('c:/temp/fizzbuzz_report.txt'),'w') numbers = ['1','2','3','4'] fileHandlerWrapperMock.write(numbers) fileHandlerWrapperMock.close() From 976d272b1185f8d6467a5ecba1639a59884fae37 Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 18:49:02 +0530 Subject: [PATCH 4/7] Update master --- FizzBuzz.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..a166f7d 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,18 +1,21 @@ -""" 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 +34,4 @@ def report(self, numbers): if "__main__" == __name__: fb = FizzBuzz() - fb.report(range(100)) - - + fb.report(range(100)) \ No newline at end of file From 40d57f438fb1aa887ce9c832bf931bf78302167b Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 18:51:07 +0530 Subject: [PATCH 5/7] Update master --- TestFizzBuzzStubbed.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 8fbfa42..b920085 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -3,7 +3,6 @@ import FizzBuzz """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] - Ans: setUpClass FizzBuzzStubbed @@ -15,15 +14,30 @@ 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 @@ -46,11 +60,18 @@ 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) + 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() From 26b0ede05ea896bae6efc4864c96f63d505cfb35 Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 18:53:05 +0530 Subject: [PATCH 6/7] Update master --- FizzBuzz.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index a166f7d..77186a3 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,12 +1,9 @@ +""" 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): From d9b71fa8d6069b1f74dcc530164d0d7c790bbd8b Mon Sep 17 00:00:00 2001 From: gauravisomani Date: Tue, 26 Jun 2012 18:57:08 +0530 Subject: [PATCH 7/7] Update master --- TestFizzBuzzStubbed.py | 1 + 1 file changed, 1 insertion(+) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index b920085..d395b90 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -64,6 +64,7 @@ def test_report(self): 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"