TestCenter Reference
UnitTestSupport.py
Go to the documentation of this file.
1 # **InsertLicense** code author="Jonas Singe"
2 # This module contains methods that are handy when using the python unittest framework within the TestCenter.
3 # -------------------------------------------------------------------------------
4 
5 import unittest
6 import inspect
7 import os
8 
9 
10 class TestCaseWithSupportData(unittest.TestCase):
11  """
12  This is a helper class that can be used instead of TestSupport.Base.getDataDirectory()
13  It allows you to access the Tests Data directory from a unittest, no matter whether it's wrapped in a TestCenter test
14  or whether you call it directly from python.
15  In the latter case, the Data directory is expected to be in the folder where your python TestCase is defined.
16  To access the folder, use self.getTestSupportDataDirectory() or self.getFileInTestSupportDataDirectory("example.txt")
17 
18  E.g.:
19  MyTestCase
20  - MyTestCase.def
21  - MyTestCase.py
22  - Data
23  - exampleInput.txt
24  """
25 
26  @classmethod
28  return _getDataDirectory(cls)
29 
30  @classmethod
32  return os.path.join(cls.getTestSupportDataDirectory(), filename)
33 
34 
35 def _isTestHelperAvailable():
36  try:
37  from TestSupport.Base import TestHelper
38  TestHelper()
39  return True
40  except (ImportError, Exception):
41  return False
42 
43 if _isTestHelperAvailable():
44  from TestSupport.Base import getDataDirectory as getTestSupportDataDirectory
45 
46  def _getDataDirectory(testCaseClass):
47  # inspect.getfile() would not work inside MeVisLab, therefore rely on the TestSupport function:
49 else:
50  def _getDataDirectory(testCaseClass):
51  sourcePath = inspect.getfile(testCaseClass)
52  return os.path.join(os.path.dirname(sourcePath), "Data")
53 
Basic support functions.
Definition: Base.py:1
A singleton to provide important data for the testing process.