minitest/minitest.py

348 lines
12 KiB
Python
Executable File

#!/usr/bin/env python
# Copyleft (c) 2016 Cocobug All Rights Reserved.
from blessings import Terminal
import types,string
__all__ =["testGroup","testUnit"]
SUCCESS_STATUS=1
FAILURE_STATUS=2
WARNING_STATUS=4
CRITICAL_STATUS=8
class testCoreOutOfTests(IndexError):
pass
class testGroup(object):
"""TestGroup, group a number of testUnit, exec them and print the results"""
def __init__(self,name="",terminal=None,prefix="",verbose=False,align=0):
self._tests=[]
self.name=name
self.t=terminal
self.prefix=prefix
if self.t==None:
self.t=Terminal()
self.results=[]
self.success_text= self.t.green("success")
self.failure_text=self.t.bright_red("failure")
self.warning_text=self.t.yellow("warning")
self.critical_text=self.t.bold_bright_red("critical")
self.verbose=verbose
self.align=align
def addTest(self,testUnit):
self._tests.append(testUnit)
def test(self):
"Execute all tests, some options might exist at some point"
module_success,module_total=0,0
status_modules={
"success":0,
"total":0,
"warning":0,
"critical":0,
"failure":0
}
print(self.prefix+"+ Executing test group "+self.pretty_group(self.name))
oldprefix=self.prefix
self.prefix+="| "
self.results=[]
for test in self._tests:
try:
list_status,total,log_results=self.print_result(test.test())
except:
list_status,total,log_results=self.print_result([])
print(self.pretty_subtests(test.name,list_status,total))
if list_status["success"]+list_status["warning"]==total:
status_modules["success"]+=1
status_modules["total"]+=1
for log in log_results:
print(log)
self.results.append([self,SUCCESS_STATUS,""])
self.prefix=oldprefix
print(self.pretty_group_result(status_modules,status_modules["total"]))
return self.results
def print_result(self,table):
"Get the array of success/failures and print according to the options (still none yet)"
total=len(table)
success=0
results_array=[]
nb=0
list_status={
"success":0,
"failure":0,
"warning":0,
"critical":0
}
for item,status,infos in table:
nb+=1
if status==SUCCESS_STATUS:
list_status["success"]+=1
elif status==WARNING_STATUS:
list_status["warning"]+=1
elif status==CRITICAL_STATUS:
list_status["critical"]+=1
else:
list_status["failure"]+=1
if self.verbose or status!=SUCCESS_STATUS:
results_array.append(self.pretty_result(status,nb,item,infos))
return list_status,total,results_array
def pretty_group_result(self,module_status,total):
"Prettyfying the result of the batch of tests"
bloc=self.prefix+"+ Done "
return bloc+self.pretty_group(self.name)+self.pretty_dots(bloc,len(self.name))+self.pretty_successrate(module_status,total)
def pretty_name(self,item):
"Just a pretty way of showing the name of a test"
try:
return item.__name__.strip("<>")
except:
return str(item)
def pretty_subtests(self,name,success,total):
"Pretty way of showing the result of the group of tests"
bloc=self.prefix+"testing "+ self.pretty_test(name)
return bloc+self.pretty_dots(bloc)+self.pretty_successrate(success,total)
def pretty_result(self,status,nb,item,infos):
"Just a pretty way of showing the result of one test"
bloc=self.prefix+" * "+" ["+str(nb)+"] "+self.pretty_name(item)
if status==CRITICAL_STATUS:
pbloc=self.prefix+" * "+" ["+str(nb)+"] "+self.t.bold_bright_red(self.pretty_name(item))
else:
pbloc=bloc
return pbloc+self.pretty_dots(bloc)+self.pretty_status(status)+self.pretty_info(infos)
def pretty_dots(self,bloc,padding=0):
lenbloc=len(bloc)+padding
if (self.align>lenbloc+2):
dots="."*(self.align-lenbloc)
else:
dots=".."
return dots
def pretty_info(self,infos):
"Prettyfy the additional infos"
if infos=="":
return ""
return self.t.italic(" ("+str(infos)+")")
def pretty_status(self,status):
"Prettyfy the status of the test"
if status==SUCCESS_STATUS:
return self.success_text
elif status==FAILURE_STATUS:
return self.failure_text
elif status==WARNING_STATUS:
return self.warning_text
else:
return self.critical_text
def pretty_group(self,name):
"Prettify the name of the testGroup"
return self.t.bold(name)
def pretty_test(self,test):
"Prettify the name of the testUnit"
return test
def pretty_successrate(self,success,total):
warnings=""
if success["success"]==total:
wrap=self.t.green
txt=self.success_text
elif success["success"]+success["warning"]==total:
wrap=self.t.yellow
txt=self.warning_text
warnings=" ({} warnings".format(str(success["warning"]))
elif success["critical"]!=0:
warp=self.t.white_on_red
text=self.critical_text
else:
wrap=self.t.bright_red
txt=self.failure_text
return wrap(txt+" ["+str(success["success"]+success["warning"])+"/"+str(total)+"]"+warnings)
def __str__(self):
return self.name
class testUnit(object):
"""A very basic test unit, only test() is mandatory
test(): should return a list of [function,status,infos]
Function added as test should raise error if incorect (assert is your friend),
you can customise the test unit in any way that the field info become actually informative
The whole class can (should) be modified to hold the test environement
"""
def __init__(self,name):
"The name if the name of the group of test executed"
self._tests=[]
self.name=name
self.results=[]
def addTest(self,test):
"Add a function to the test unit"
self._tests.append(test)
def test(self):
"Execute all tests"
self.results=[]
for test in self._tests:
try:
test()
self.addResult(test,SUCCESS_STATUS,'')
except Exception as e:
self.addResult(test,FAILURE_STATUS,e)
return self.results
def addResult(self,testName,status,info):
self.results.append([testName,status,info])
class simpleTestUnit(testUnit):
"""Very simple test function, it tries to autodetect tests and add them and batch test them,
this is not default behavior and should not be expected from other functions
The very usage of this self.list_ongoing_tests makes the class non embetted effect protected and non-thread-friendly
Note that using list_ongoing_tests and addResult together can lead to unpredictable results, use addSuccess and addFailure instead"""
def __init__(self,name):
super(simpleTestUnit, self).__init__(name)
self.results=[]
self.list_ongoing_tests=[]
self.userTests=[]
self._simpleTestList=[]
def currentTest(self,name=None):
"Get the current test"
if name==None:
try:
return self.list_ongoing_tests.pop()
except IndexError:
raise testCoreOutOfTests
else:
return self.list_ongoing_tests.append(name)
def addSuccess(self):
"Mark the success of the current test (named in the function by self.currentTest)"
self.addResult(self.currentTest(),SUCCESS_STATUS,"")
def addFailure(self,msg):
"Mark the failure of the current test"
self.addResult(self.currentTest(),FAILURE_STATUS,msg)
def addCritical(self,name,msg):
self.addResult(name,CRITICAL_STATUS,msg)
def addWarning(self,msg=""):
self.addResult(self.currentTest(),WARNING_STATUS,msg)
def test(self):
"""User can add functions to be tested in userTests,
the functions should use self.addSuccess() and self.addFailure("") to keep track of the results
test() will then proceed to autodetect all other tests"""
self.results=[]
self._simpleTestList=self.userTests[:]
for method in dir(self):
if method.startswith("_test"):
method=getattr(self,method)
if isinstance(method,types.FunctionType) or isinstance(method,types.MethodType):
self._simpleTestList.append(method)
try:
for fonct in self._simpleTestList:
fonct()
except testCoreOutOfTests:
self.addCritical("core:critical","a fatal error occured in test generation line")
except Exception as e:
self.addFailure(e)
return self.results
if __name__ == '__main__':
term=Terminal()
mainClasses=testGroup("Main Classes",term,verbose=True,align=40)
subclass=testGroup("Subgroup",term,"| ",verbose=True,align=40)
mainTest=testUnit("lambda functions")
mainTest.addTest(lambda :True)
mainTest.addTest(lambda :True)
lambdaTest=testUnit("incorrect lambdas")
lambdaTest.addTest(lambda x:True)
lambdaTest.addTest(lambda x,y:True)
class newTestUnit(testUnit):
"""Just a custom test Class, simple and easyest ?"""
def __init__(self):
super(newTestUnit, self).__init__("custom test")
def test(self):
self.results=[]
# Now all the customs test happen, and are append to the results
potatoe=[]
try: # You are supposed to fail this step
if potatoe[1]==2:
self.addResult("empty_table",self.FAILURE_STATUS,"Non empty array")
except:
self.addResult("empty_table",SUCCESS_STATUS,"")
potatoe.append([2]*2) # Not try protected, but could be if a bug was a possibility
try:
goodinit=True
for e in potatoe:
if e==3:
goodinit=False
self.addResult("init_table",FAILURE_STATUS,"Wrong elements")
if goodinit:
self.addResult("init_table",SUCCESS_STATUS,"")
except:
self.addResult("init_table",FAILURE_STATUS,"???")
return self.results
class anotherTest(simpleTestUnit):
"""docstring for anotherTest."""
def __init__(self):
super(anotherTest, self).__init__("YET ANOTHER TEST")
def _testCustom(self):
self.currentTest("testing true")
if True:
self.addSuccess()
else:
self.addFailure("True is False")
self.currentTest("additions:simple")
if 1+3==4:
self.addSuccess()
else:
self.addFailure("1+3 != 4")
self.currentTest("error")
if False:
self.addSuccess()
else:
self.addFailure("Supposed to fail")
mainTest.test()
lambdaTest.test()
mainClasses.addTest(lambdaTest)
subclass.addTest(mainTest)
mainClasses.addTest(subclass)
mainClasses.addTest(newTestUnit())
mainClasses.addTest(anotherTest())
mainClasses.test()