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
9 changes: 6 additions & 3 deletions FizzBuzz.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""
Q1. Why is the report method untestable ? [2 pts]


External collaborator directly file pointer is given.instead wrapper func should be used - FileHandleWrapper or pass an arg as fileopener.


Q2. How will you change the api of the report method to make it more testable ? [2 pts]


as mentioned above.changes done in below code :


"""
class FizzBuzz(object):
def report(self, numbers):
def report(self, numbers, FileOpener):
FileOpener.Open()

report_file = open('c:/temp/fizzbuzz_report.txt', 'w')
report_file = FileOpener('c:/temp/fizzbuzz_report.txt', 'w')

for number in numbers:
msg = str(number) + " "
Expand Down
26 changes: 12 additions & 14 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@ def tearDown(self):
self.fb = None

def test_report(self):
pass

#mock the file handle
MockInter = self.mock()
MockFile = self.mock()
self.expectAndReturn(MockInter.open('c:/temp/fizzbuzz_report.txt', 'w'), MockFile)
MockFile.write("3 fizz \n")
MockFile.close()

#replay
self.replay()

#call API
self.fb.report(numbers, FileOpener=MockInter.open)














# verify
self.verify()

if __name__ == "__main__":
unittest.main()
23 changes: 15 additions & 8 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
"""
Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts]


#class setup - at start setUp
Class FizzBuzzStubbed
#test setup before each testsetup
test_reportteardown
#test setup before each test.setup
test_reportteardown
#class teardown - at end
.tearDownClass



Expand All @@ -17,16 +24,16 @@

"""
class MyStub(object):
pass





def __init__(self):
self.values = []

def write(self, value):
self.values.append(value)

def close(self):
self.closed = True


class TestFizzBuzzStubbed(unittest.TestCase):

@classmethod
Expand Down