OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
testutils.py
Go to the documentation of this file.
1 def RunTests():
2  '''
3  This function behaves as a custom TestLoader for python unittests.
4 
5  With no system arguments, the default unittest TestRunner is used.
6 
7  If the first system argument (sys.argv[1]) is set to 'xml', a XMLTestRunner
8  is used, which produces a JUnit compatible XML output file. Within the current
9  module, each function is identified which is a subclass of unittest.TestCase
10  and for each TestCase, a test suite is executed, producing an individual
11  output file for each TestCase. The output file has the name,
12  'PYTEST-<TestCaseName>.xml'.
13 
14  Example of a Python testcase:
15 
16  .. code-block:: python
17 
18  import unittest
19 
20  class TestRenumber(unittest.TestCase):
21 
22  def setUp(self):
23  # prepare stuff"
24  pass
25 
26  def testSomeFunction(self):
27  # do some asserts
28  pass
29 
30  if __name__ == "__main__":
31  from ost import testutils
32  testutils.RunTests()
33 
34  '''
35  import unittest
36  import sys
37  try:
38  if len(sys.argv)>1 and sys.argv[1]=='xml':
39  import inspect
40  import types
41  import __main__
42  from ost import xmlrunner
43  for name, obj in inspect.getmembers(__main__):
44  if (isinstance(obj, (type, types.ClassType)) and
45  issubclass(obj, unittest.TestCase)):
46  suite = unittest.TestLoader().loadTestsFromTestCase(obj)
47  stream = open('PYTEST-%s.xml'%name, 'w')
48  xmlrunner.XMLTestRunner(stream).run(suite)
49  stream.close()
50 
51  else:
52  unittest.main()
53  except Exception, e:
54  print e