Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions FizzBuzz.py
Original file line number Diff line number Diff line change
@@ -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.



Expand All @@ -10,27 +11,24 @@

"""
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
if number % 3 == 0:
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()
12 changes: 12 additions & 0 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
63 changes: 58 additions & 5 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down