TestCenter Reference
UnitTestDiscovery.py
Go to the documentation of this file.
1 #
2 # Copyright 2017, MeVis Medical Solutions AG
3 #
4 # The user may use this file in accordance with the license agreement provided with
5 # the Software or, alternatively, in accordance with the terms contained in a
6 # written agreement between the user and MeVis Medical Solutions AG.
7 #
8 # For further information use the contact form at https://www.mevislab.de/contact
9 #
10 
11 import os
12 import unittest
13 
14 
15 def getSuiteFromPackage(unitTestRootPackage, pattern='test*.py'):
16  """
17  Creates a Test Suite from all the tests inside a python package which is located in the MeVisLab Repository.
18 
19  Example usage inside a MeVisLab TestCenter Test:
20 
21  from TestSupport.UnitTestDiscovery import getSuiteFromPackage
22  import my_module.core.tests
23 
24 
25  def UNITTEST_My_Module():
26  return getSuiteFromPackage(my_module.core.tests)
27 
28  :param unitTestRootPackage: the python package in which unit tests are searched
29  :type unitTestRootPackage: ModuleType
30  :param pattern: Only test files that match the pattern will be loaded.
31 
32  More details can be found in the documentation of the TestLoader.discover() function.
33  """
34  searchPath = unitTestRootPackage.__path__[0]
35  top_level_dir = _getTopLevelDir(searchPath)
36  # The top level dir is passed to the discover method to avoid name conflicts
37  # These would otherwise show up when modules with the same names were imported from different packages
38  return unittest.TestLoader().discover(searchPath, pattern, top_level_dir=top_level_dir)
39 
40 
41 def _getTopLevelDir(searchPath):
42  pythonFolder = os.path.join("Scripts", "python")
43  top_level_dir = searchPath[0:searchPath.find(pythonFolder) + len(pythonFolder)]
44  return top_level_dir
45 
46 
47 def getSuiteFromFolder(searchPath, pattern='*.py'):
48  """
49  Creates a Test Suite from all the tests inside a folder, this could be a TestCenter TestCase folder.
50  :param searchPath: the folder where to search for the unit tests.
51  :param pattern: Only test files that match the pattern will be loaded.
52  """
53  return unittest.TestLoader().discover(searchPath, pattern)
def getSuiteFromPackage(unitTestRootPackage, pattern='test *.py')
def getSuiteFromFolder(searchPath, pattern=' *.py')