diff --git a/README.md b/README.md index 1738eaa..ee9fa5e 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ minitest + + +Beggening test for TestGroup + Testing addTest....SUCESS [5/5] + Testing fidget.....FAILURE [3/5] + * test 2 functionNAME@0x12000 AssertError + * test 4 functionNAME@0x12000 ValueError + Beggening test for Subgroup + Testing none.....SUCESS [1/1] + End test for Subgroup [1/1] +End test for TestGroup [2/3] diff --git a/minitest.py b/minitest.py new file mode 100755 index 0000000..48d3045 --- /dev/null +++ b/minitest.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +# Copyleft (c) 2016 Cocobug All Rights Reserved. + +from blessings import Terminal + +class testGroup(object): + """TestGroup, group a number of testUnit, exec them and print the results""" + def __init__(self,name="",terminal=None,prefix=""): + self._tests=[] + self.name=name + self.t=terminal + self.prefix=prefix + if self.t==None: + self.t=Terminal() + + def addTest(self,testUnit): + self._tests.append(testUnit) + + def test(self): + "Execute all tests, some options might exist at some point" + sucess,total=0,0 + print self.prefix+"Beggining test group "+self.pretty_group(self.name) + for test in self._tests: + print self.prefix+"Testing"+ self.pretty_test(name) + sucess,total=self.print_result(test.test()) + print self.prefix+"Test finished for test group "+self.pretty_group(self.name)+" "+self.pretty_succesrate(sucess,total) + + def print_result(self,table): + "Get the array of sucess/failures and print according to the options (still none yet)" + total=len(table) + sucess=0 + for item,status,infos in table: + if status: + sucess+=1 + print self.prefix+item+"..."+self.pretty_status(status)+self.pretty_info(infos) + return sucess,total + + def pretty_info(self,infos): + "Prettyfy the additional infos" + return self.t.italic(infos) + + def pretty_status(self,status): + "Prettyfy the status of the test" + if status: + return self.t.green("ok") + return self.t.red("fail") + + 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 self.t.underline(test) + + def pretty_succesrate(self,sucess,total): + if sucess==total: + wrap=self.t.green + else: + wrap=self.t.red + return wrap("["+str(sucess)+"/"+str(total)+"]") + +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.test=[] + + def addTest(self,test): + "Add a function to the test unit" + self._tests.append(test) + + def test(self): + "Execute all tests" + for test in self._tests: + try: + test() + self.results.append(test,True,'') + except as e: + self.results.append(test,False,e) + + +if __name__ == '__main__': + term=Terminal() + mainClasses=testGroup("Main Classes",term) + #mainClasses.addTest() + #mainClasses.test() + testClasses=testUnit("Main classes")