Package test_suite :: Package unit_tests :: Module unit_test_runner
[hide private]
[frames] | no frames]

Module unit_test_runner

source code

Utilities for unit test running from the command line or within the relax testing frame work.

Unit tests in the relax frame work are stored in a directory structure rooted at <relax-root-directory>/test_suite/unit_tests. The directory unit tests contains a directory structure that mirrors the relax directory structure and which ideally contains one unit test file/module for each file/module in the relax framework. The default convention is that the unit test module for a relax module called <relax-module> is called test_<relax-module> (stored in test_<relax-module>.py). The unit test module test_<relax-module> should then contain a class called Test_<relax-module> which is a child of TestCase and contains methods whose names start with 'test' and take no arguments other than self.

A concrete example: for class <relax-root-directory>/maths-fns/chi2.py FIXME:***complete***

The framework can discover sets of unit tests from the file system and add them to TestSuites either from the command line or programmatically from inside another program. It also has the ability to search for a root unit test and system directory from a position anywhere inside the unit test hierarchy.

TODO: Examine PEP 338 and runpy.run_module(modulename): Executing Modules as Scripts for a later version of relax that is dependant on python 2.5. TODO: Split out runner part from search part.

Classes [hide private]
  ImportErrorTestCase
TestCase class for nicely handling import errors.
  Test_finder
Find and load unit test classes as a hierarchy of TestSuites and TestCases.
  Unit_test_runner
Class to run a particular unit test or a directory of unit tests.
Functions [hide private]
str
get_startup_path()
Get the path of the directory the program started from.
source code
list of class module instances or None
import_module(module_path)
Import the python module named by module_path.
source code
str or None
get_module_relative_path(package_path, module_name, root_paths=None)
Find the relative path of a module to one of a set of root paths using a list of package paths and a module name.
source code
list of str
get_common_prefix(path1, path2)
Get the common prefix between two paths.
source code
list of str
segment_path(path, normalise=True)
Segment a path into a list of components (drives, files, directories etc).
source code
str
join_path_segments(segments)
Join a list of path segments (drives, files, directories etc) into a path.
source code
TestSuite instance
load_test_case(package_path, module_name, class_name)
Load a testCase from the file system using a package path, file name and class name.
source code
Variables [hide private]
  PY_FILE_EXTENSION = '.py'
  __package__ = 'test_suite.unit_tests'

Imports: copy, os, re, sys, unittest, traceback, OptionParser, dedent, TestLoader


Function Details [hide private]

get_startup_path()

source code 

Get the path of the directory the program started from.

The startup path is the first path in sys.path (the internal PYTHONPATH) by convention. If the first element of sys.path is an empty trying the current working directory is used instead.

Returns: str
A file system path for the current operating system.

import_module(module_path)

source code 

Import the python module named by module_path.

Parameters:
  • module_path (str) - A module path in python dot separated format. Note: this currently doesn't support relative module paths as defined by pep328 and python 2.5.
Returns: list of class module instances or None
The module path as a list of module instances or None if the module path cannot be found in the python path.

get_module_relative_path(package_path, module_name, root_paths=None)

source code 

Find the relative path of a module to one of a set of root paths using a list of package paths and a module name.

As the module may match more than one path the first path that can contain it is chosen.

Parameters:
  • package_path (str) - Path of a python packages leading to module_name.
  • module_name (str) - The name of the module to load.
  • root_paths (list of str) - A set of paths to search for the module in. If None is passed the list is initialized from the internal PYTHONPATH sys.path. Elements which are empty strings are replace with the current working directory sys.getcwd().
Returns: str or None
A relative module path to one of the rootPaths which is separated by '.'s if the modulePath is a subpath of one of the root paths, otherwise None.

get_common_prefix(path1, path2)

source code 

Get the common prefix between two paths.

Parameters:
  • path1 (list of str) - The first path to be compared.
  • path2 (list of str) - The second path to be compared.
Returns: list of str
The common path shared between the two paths starting from the root directory as a list of segments. If there is no common path an empty list is returned.

segment_path(path, normalise=True)

source code 

Segment a path into a list of components (drives, files, directories etc).

Parameters:
  • path (str) - The path to segment.
  • normalise (bool) - Whether to normalise the path before starting.
Returns: list of str
A list of path segments.

join_path_segments(segments)

source code 

Join a list of path segments (drives, files, directories etc) into a path.

Parameters:
  • segments (a list of path segments) - The path segments to join into a path.
Returns: str
The path containing the joined path segments.

load_test_case(package_path, module_name, class_name)

source code 

Load a testCase from the file system using a package path, file name and class name.

Parameters:
  • package_path (str) - Full system path of the module file.
  • module_name (str) - Name of the module to load the class from.
  • class_name (str) - Name of the class to load.
Returns: TestSuite instance
The suite of test cases.