00001 def RunTests():
00002 '''
00003 This function behaves as a custom TestLoader for python unittests.
00004
00005 With no system arguments, the default unittest TestRunner is used.
00006
00007 If the first system argument (sys.argv[1]) is set to 'xml', a XMLTestRunner
00008 is used, which produces a JUnit compatible XML output file. Within the current
00009 module, each function is identified which is a subclass of unittest.TestCase
00010 and for each TestCase, a test suite is executed, producing an individual
00011 output file for each TestCase. The output file has the name,
00012 'PYTEST-<TestCaseName>.xml'.
00013
00014 Example of a Python testcase:
00015
00016 .. code-block:: python
00017
00018 import unittest
00019
00020 class TestRenumber(unittest.TestCase):
00021
00022 def setUp(self):
00023 # prepare stuff"
00024 pass
00025
00026 def testSomeFunction(self):
00027 # do some asserts
00028 pass
00029
00030 if __name__ == "__main__":
00031 from ost import testutils
00032 testutils.RunTests()
00033
00034 '''
00035 import unittest
00036 import sys
00037 try:
00038 if len(sys.argv)>1 and sys.argv[1]=='xml':
00039 import inspect
00040 import types
00041 import __main__
00042 from ost import xmlrunner
00043 for name, obj in inspect.getmembers(__main__):
00044 if (isinstance(obj, (type, types.ClassType)) and
00045 issubclass(obj, unittest.TestCase)):
00046 suite = unittest.TestLoader().loadTestsFromTestCase(obj)
00047 stream = open('PYTEST-%s.xml'%name, 'w')
00048 xmlrunner.XMLTestRunner(stream).run(suite)
00049 stream.close()
00050
00051 else:
00052 unittest.main()
00053 except Exception, e:
00054 print e