diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..7c16116 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -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) + " " diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..fb9009e 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -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): @@ -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() + + diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..96b8b6a 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -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 + @@ -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):