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
00055
00056
00057 def SetDefaultCompoundLib():
00058 '''
00059 This function tries to ensure that a default compound library is set.
00060 When calling scripts with ``ost`` this is not needed, but since unit tests are
00061 called with ``python`` we need to ensure that it is set. The function is only
00062 expected to succeed (and return True) if ``COMPOUND_LIB`` was set when
00063 :ref:`configuring the compilation <cmake-flags>`.
00064
00065 It tries the following:
00066
00067 - get it with :func:`ost.conop.GetDefaultLib`
00068 - look for ``compounds.chemlib`` in ``$OST_ROOT/share/openstructure``
00069 - if ``OST_ROOT`` not set in the above, try to guess it based on the path of
00070 the ``conop`` module
00071
00072 To use this check modify the :func:`RunTests` call to read:
00073
00074 .. code-block:: python
00075
00076 if __name__ == "__main__":
00077 from ost import testutils
00078 if testutils.SetDefaultCompoundLib():
00079 testutils.RunTests()
00080 else:
00081 print 'No compound library available. Ignoring test_XXX.py tests.'
00082
00083 :return: True, if a compound library was found and set to be accessed with
00084 :func:`ost.conop.GetDefaultLib`. False otherwise.
00085 '''
00086 import os
00087 from ost import conop, GetSharedDataPath, SetPrefixPath
00088
00089 if conop.GetDefaultLib():
00090 return True
00091 else:
00092
00093 try:
00094 shared_data_path = GetSharedDataPath()
00095 except Exception, e:
00096 SetPrefixPath(os.path.abspath(os.path.join(conop.__path__[0], os.pardir,
00097 os.pardir, os.pardir,
00098 os.pardir, os.pardir)))
00099 shared_data_path = GetSharedDataPath()
00100
00101 compound_lib_path = os.path.join(shared_data_path, 'compounds.chemlib')
00102 if os.path.exists(compound_lib_path):
00103 compound_lib = conop.CompoundLib.Load(compound_lib_path)
00104 conop.SetDefaultLib(compound_lib)
00105 return True
00106 else:
00107 return False