import sys
class Capture(object):
output = ""
def write(self, output):
self.output += output
def testFunctionSTDOUT(function, exspectedvalue):
c = Capture()
sys.stdout = c
function()
sys.stdout = sys.__stdout__
if c.output == exspectedvalue:
return True
else:
return False
def testFunctionSTDOUT2(function, functionArguments={}, exspectedvalue=""):
c = Capture()
sys.stdout = c
function(**functionArguments)
sys.stdout = sys.__stdout__
if c.output == exspectedvalue:
return True
else:
return False
#test to see if it works
def testfunction():
print ("text to capture")
def testfunction2(stuff):
print (stuff)
print(testFunctionSTDOUT(testfunction, "text to capture\n"))
print(testFunctionSTDOUT(testfunction, "this should fail\n"))
print(testFunctionSTDOUT2(testfunction2, {"stuff":"text to capture"} , "text to capture\n"))
print(testFunctionSTDOUT2(testfunction2, {"stuff":"text to capture"} , "this should fail\n"))