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
53 changes: 52 additions & 1 deletion FizzBuzz.py
Original file line number Diff line number Diff line change
@@ -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()



"""
Expand Down
17 changes: 13 additions & 4 deletions TestFizzBuzzMocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()



Expand All @@ -36,5 +46,4 @@ def test_report(self):



if __name__ == "__main__":
unittest.main()

27 changes: 18 additions & 9 deletions TestFizzBuzzStubbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand All @@ -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
Expand Down