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
55 
56 
58  '''
59  This function tries to ensure that a default compound library is set.
60  When calling scripts with ``ost`` this is not needed, but since unit tests are
61  called with ``python`` we need to ensure that it is set. The function is only
62  expected to succeed (and return True) if ``COMPOUND_LIB`` was set when
63  :ref:`configuring the compilation <cmake-flags>`.
64 
65  It tries the following:
66 
67  - get it with :func:`ost.conop.GetDefaultLib`
68  - look for ``compounds.chemlib`` in ``$OST_ROOT/share/openstructure``
69  - if ``OST_ROOT`` not set in the above, try to guess it based on the path of
70  the ``conop`` module
71 
72  To use this check modify the :func:`RunTests` call to read:
73 
74  .. code-block:: python
75 
76  if __name__ == "__main__":
77  from ost import testutils
78  if testutils.SetDefaultCompoundLib():
79  testutils.RunTests()
80  else:
81  print 'No compound library available. Ignoring test_XXX.py tests.'
82 
83  :return: True, if a compound library was found and set to be accessed with
84  :func:`ost.conop.GetDefaultLib`. False otherwise.
85  '''
86  import os
87  from ost import conop, GetSharedDataPath, SetPrefixPath
88  # check if already there
89  if conop.GetDefaultLib():
90  return True
91  else:
92  # try to get the shared data path?
93  try:
94  shared_data_path = GetSharedDataPath()
95  except Exception, e:
96  SetPrefixPath(os.path.abspath(os.path.join(conop.__path__[0], os.pardir,
97  os.pardir, os.pardir,
98  os.pardir, os.pardir)))
99  shared_data_path = GetSharedDataPath()
100  # look for compounds.chemlib in there
101  compound_lib_path = os.path.join(shared_data_path, 'compounds.chemlib')
102  if os.path.exists(compound_lib_path):
103  compound_lib = conop.CompoundLib.Load(compound_lib_path)
104  conop.SetDefaultLib(compound_lib)
105  return True
106  else:
107  return False
def RunTests
Definition: testutils.py:1
void DLLEXPORT_OST_BASE SetPrefixPath(const String &prefix)
set path prefix
String DLLEXPORT_OST_BASE GetSharedDataPath()
def SetDefaultCompoundLib
Definition: testutils.py:57