diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..b42e713 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,11 +1,62 @@ """ Q1. Why is the report method untestable ? [2 pts] - +Ans - + 1. As the file path is user/OS/System specific + 2. inputs to report method depends on colabrataive (Range) + 3. While writing msg to a report file its constructed from numbers. + 4. it is easy to use for writer but not to user who is using it. Q2. How will you change the api of the report method to make it more testable ? [2 pts] +Ans :- + 1. For handelling the file pale we wili use file handler + +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 + + if fizzbuzz_found: + + file_handle.write(msg + "\n") + + + + + + + + + +if "__main__" == __name__: + + fb = FizzBuzz() + + file_handle = open('temp.txt', 'w') # can create open wrapper + + fb.report(range(100), file_handle) + + file_handle.close() + """ diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..b1fe3f8 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -6,8 +6,9 @@ mock objects to test the report method of FizzBuzz. [5 pts] """ class TestFizzBuzzMocked(pymock.PyMockTestCase): - + def setUp(self): + super(TestFizzBuzzMocked, self).setUp() self.fb = FizzBuzz.FizzBuzz() print "setUp TestFizzBuzzMocked" @@ -17,11 +18,20 @@ def tearDown(self): self.fb = None def test_report(self): - pass + #create mock + mockFileProvider = self.mock() + mockFileWrapper = self.mockFileProvider('report.txt', 'w') + #replay + self.replay() + #Call the report method + numbers=range(100) + FizzBuzz.report(self.numbers,self.mockFileWrapper) +if __name__ == "__main__": + unittest.main() @@ -36,5 +46,4 @@ def test_report(self): -if __name__ == "__main__": - unittest.main() + diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..0abc2dd 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,10 +4,15 @@ """ 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 @@ -17,16 +22,20 @@ """ class MyStub(object): - pass - - - + def __init__(self): + self.openflag=False + self.values=[] # create empty list + def open(self): + self.openflag = True #"open fake file" + def write(self,msg): + self.values.append(msg) # append values to list + def close(): + self.openflag = False # close fake "file" - class TestFizzBuzzStubbed(unittest.TestCase): @classmethod