Skip to content

Latest commit

 

History

History
63 lines (50 loc) · 2.07 KB

ok.md

File metadata and controls

63 lines (50 loc) · 2.07 KB


At a glance... | Syllabus | Models | Code | Lecturer

Unit tests in Python

Python has some great unit testing tools. The one shown below is a "less-is-more" approach and is based on Kent Beck video on how to write a test engine in just a few lines of code.

For example usages, see okok.py which can be loaded via

python okok.py

Share and enjoy.


   1:   def ok(*lst):
   2:     print "### ",lst[0].__name__
   3:     for one in lst: unittest(one)
   4:     return one
   5:   
   6:   class unittest:
   7:     tries = fails = 0  #  tracks the record so far
   8:     @staticmethod
   9:     def enough():
  10:       print(unittest.score()); exit()
  11:     @staticmethod
  12:     def score():
  13:       t = unittest.tries
  14:       f = unittest.fails
  15:       return "# TRIES= %s FAIL= %s %%PASS = %s%%"  % (
  16:         t,f,int(round(t*100/(t+f+0.001))))
  17:     def __init__(i,test):
  18:       unittest.tries += 1
  19:       try:
  20:         test()
  21:       except Exception,e:
  22:         unittest.fails += 1
  23:         i.report(test)
  24:     def report(i,test):
  25:       import traceback
  26:       print traceback.format_exc()
  27:       print unittest.score(),':',test.__name__

Copyright © 2015 Tim Menzies. This is free and unencumbered software released into the public domain.
For more details, see the license.