PageRenderTime 57ms CodeModel.GetById 48ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/libtest/UnitTest.py

http://pyjamas.googlecode.com/
Python | 126 lines | 111 code | 14 blank | 1 comment | 3 complexity | b616a09de09212dda0f42eaaecf190bb MD5 | raw file
  1from write import write, writebr
  2
  3class UnitTest:
  4	def __init__(self):
  5		self.tests_completed=0
  6		self.tests_failed=0
  7		self.tests_passed=0
  8		self.test_methods=[]
  9		
 10		# define alternate names for methods

 11		self.assertEqual = self.failUnlessEqual
 12		self.assertEquals = self.failUnlessEqual
 13
 14		self.assertNotEqual = self.failIfEqual
 15		self.assertFalse = self.failIf
 16		self.assertTrue = self.failUnless
 17
 18	def run(self):
 19		self.getTestMethods()
 20		for test_method_name in self.test_methods:
 21			test_method=getattr(self, test_method_name)
 22			self.setUp()
 23			test_method()
 24			self.tearDown()
 25		
 26		self.displayStats()
 27		
 28	def setUp(self):
 29		pass
 30		
 31	def tearDown(self):
 32		pass
 33	
 34	def getName(self):
 35		return ""
 36
 37	def getNameFmt(self, msg=""):
 38		if self.getName():
 39			if msg:
 40				msg=" " + msg
 41			return self.getName() + msg + ": "
 42		return ""
 43		
 44	def getTestMethods(self):
 45		self.test_methods=filter(self, self.isTestMethod, dir(self))
 46		
 47	def isTestMethod(self, method):
 48		if callable(getattr(self, method)):
 49			if method.find("test")==0:
 50				return True
 51		return False
 52
 53	def fail(self, msg=None):
 54		self.tests_failed+=1
 55		
 56		if not msg:
 57			msg="assertion failed"
 58
 59		title="<b>" + self.getNameFmt("Test failed") + "</b>"
 60		writebr(title + msg)
 61		if console:
 62			console.error(msg)
 63			console.trace()
 64		return False
 65
 66	def startTest(self):
 67		self.tests_completed+=1
 68
 69	def failIf(self, expr, msg=None):
 70		self.startTest()
 71		if expr:
 72			return self.fail(msg)
 73
 74	def failUnless(self, expr, msg=None):
 75		self.startTest()
 76		if not expr:
 77			return self.fail(msg)
 78
 79	def failUnlessEqual(self, first, second, msg=None):
 80		self.startTest()
 81		if not first == second:
 82			if not msg:
 83				msg=str(first) + " != " + str(second)
 84			return self.fail(msg)
 85
 86	def failIfEqual(self, first, second, msg=None):
 87		self.startTest()
 88		if first == second:
 89			if not msg:
 90				msg=str(first) + " == " + str(second)
 91			return self.fail(msg)
 92
 93	def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
 94		self.startTest()
 95		if round(second-first, places) != 0:
 96			if not msg:
 97				msg=str(first) + " != " + str(second) + " within " + str(places) + " places"
 98			return self.fail(msg)
 99
100	def failIfAlmostEqual(self, first, second, places=7, msg=None):
101		self.startTest()
102		if round(second-first, places) == 0:
103			if not msg:
104				msg=str(first) + " == " + str(second) + " within " + str(places) + " places"
105			return self.fail(msg)
106
107	def displayStats(self):
108		if self.tests_failed:
109			bg_colour="#ff0000"
110			fg_colour="#ffffff"
111		else:
112			bg_colour="#00ff00"
113			fg_colour="#000000"
114		
115		tests_passed=self.tests_completed - self.tests_failed
116		
117		output="<table cellpadding=4 width=100%><tr><td bgcolor='" + bg_colour + "'><font face='arial' size=4 color='" + fg_colour + "'><b>"
118		output+=self.getNameFmt() + "Passed " + tests_passed + "/" + self.tests_completed + " tests"
119		
120		if self.tests_failed:
121			output+=" (" + self.tests_failed + " failed)"
122			
123		output+="</b></font></td></tr></table>"
124		
125		write(output)
126