PageRenderTime 45ms CodeModel.GetById 19ms 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
Possible License(s): LGPL-2.1, Apache-2.0
  1. from write import write, writebr
  2. class UnitTest:
  3. def __init__(self):
  4. self.tests_completed=0
  5. self.tests_failed=0
  6. self.tests_passed=0
  7. self.test_methods=[]
  8. # define alternate names for methods
  9. self.assertEqual = self.failUnlessEqual
  10. self.assertEquals = self.failUnlessEqual
  11. self.assertNotEqual = self.failIfEqual
  12. self.assertFalse = self.failIf
  13. self.assertTrue = self.failUnless
  14. def run(self):
  15. self.getTestMethods()
  16. for test_method_name in self.test_methods:
  17. test_method=getattr(self, test_method_name)
  18. self.setUp()
  19. test_method()
  20. self.tearDown()
  21. self.displayStats()
  22. def setUp(self):
  23. pass
  24. def tearDown(self):
  25. pass
  26. def getName(self):
  27. return ""
  28. def getNameFmt(self, msg=""):
  29. if self.getName():
  30. if msg:
  31. msg=" " + msg
  32. return self.getName() + msg + ": "
  33. return ""
  34. def getTestMethods(self):
  35. self.test_methods=filter(self, self.isTestMethod, dir(self))
  36. def isTestMethod(self, method):
  37. if callable(getattr(self, method)):
  38. if method.find("test")==0:
  39. return True
  40. return False
  41. def fail(self, msg=None):
  42. self.tests_failed+=1
  43. if not msg:
  44. msg="assertion failed"
  45. title="<b>" + self.getNameFmt("Test failed") + "</b>"
  46. writebr(title + msg)
  47. if console:
  48. console.error(msg)
  49. console.trace()
  50. return False
  51. def startTest(self):
  52. self.tests_completed+=1
  53. def failIf(self, expr, msg=None):
  54. self.startTest()
  55. if expr:
  56. return self.fail(msg)
  57. def failUnless(self, expr, msg=None):
  58. self.startTest()
  59. if not expr:
  60. return self.fail(msg)
  61. def failUnlessEqual(self, first, second, msg=None):
  62. self.startTest()
  63. if not first == second:
  64. if not msg:
  65. msg=str(first) + " != " + str(second)
  66. return self.fail(msg)
  67. def failIfEqual(self, first, second, msg=None):
  68. self.startTest()
  69. if first == second:
  70. if not msg:
  71. msg=str(first) + " == " + str(second)
  72. return self.fail(msg)
  73. def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
  74. self.startTest()
  75. if round(second-first, places) != 0:
  76. if not msg:
  77. msg=str(first) + " != " + str(second) + " within " + str(places) + " places"
  78. return self.fail(msg)
  79. def failIfAlmostEqual(self, first, second, places=7, msg=None):
  80. self.startTest()
  81. if round(second-first, places) == 0:
  82. if not msg:
  83. msg=str(first) + " == " + str(second) + " within " + str(places) + " places"
  84. return self.fail(msg)
  85. def displayStats(self):
  86. if self.tests_failed:
  87. bg_colour="#ff0000"
  88. fg_colour="#ffffff"
  89. else:
  90. bg_colour="#00ff00"
  91. fg_colour="#000000"
  92. tests_passed=self.tests_completed - self.tests_failed
  93. output="<table cellpadding=4 width=100%><tr><td bgcolor='" + bg_colour + "'><font face='arial' size=4 color='" + fg_colour + "'><b>"
  94. output+=self.getNameFmt() + "Passed " + tests_passed + "/" + self.tests_completed + " tests"
  95. if self.tests_failed:
  96. output+=" (" + self.tests_failed + " failed)"
  97. output+="</b></font></td></tr></table>"
  98. write(output)