diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..8aa29c7 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,6 +1,7 @@ """ Q1. Why is the report method untestable ? [2 pts] +Report_file open and path is external collaborative (platform and system environment dependent) which is handled in the function so it is untestable. @@ -10,10 +11,7 @@ """ class FizzBuzz(object): - def report(self, numbers): - - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') - + def report(self, numbers, file_handle): for number in numbers: msg = str(number) + " " fizzbuzz_found = False @@ -21,16 +19,16 @@ def report(self, numbers): msg += "fizz " fizzbuzz_found = True if number % 5 == 0: - msg += "buzz " - fizzbuzz_found = True - + msg += "buzz " + fizzbuzz_found = True if fizzbuzz_found: - report_file.write(msg + "\n") + file_handle.write(msg + "\n") - report_file.close() + -if "__main__" == __name__: - fb = FizzBuzz() - fb.report(range(100)) - +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..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): diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..14b2c66 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,17 +4,32 @@ """ 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 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] +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 @@ -27,6 +42,44 @@ class MyStub(object): +class TestFizzBuzzStubbed(unittest.TestCase): + + @classmethod + def setUpClass(cls): + print "setUpClass FizzBuzzStubbed" + + def setUp(self): + super(TestFizzBuzzStubbed, self).setUp() + self.fb = FizzBuzz.FizzBuzz() + print "setup" + + @classmethod + def tearDownClass(cls): + print "tearDownClass" + + def tearDown(self): + super(TestFizzBuzzStubbed, self).tearDown() + self.fb = None + print "teardown" + + def test_report(self): + print "test_report" + pass + + def test_report_for_empty_list(self): + print "test_report" + pass + +if __name__ == "__main__": + unittest.main() + + + + + + + + class TestFizzBuzzStubbed(unittest.TestCase): @classmethod