TestSupport.py

A support package for using the unittest module. These are routines that are commonly useful during testing. They include test filename generation, file comparisions, and some logging support.

static TestSupport.print_namespace(namespace, stream=sys.stdout, all=False)
class TestSupport.TestSupport(tester)[source]

Assemble the support routines in an object for access. My normal invocation is in the testcase.setUp() method, as

>>> def setUp(self):
>>>     self.support = TestSupport(self)
static alter_arg(arglist, flag, newvalue)[source]

Set the value of a parameter keyed by flag to newvalue

assert_re(RE, product, flags=False)[source]

helper method to use regexes, chiefly for absolute pathnames

static delete_arg(arglist, flag, unary=False)[source]

Remove a parameter flag and, (by default) its value, or just the flag if unary = True

static list_namespace(namespace, all=False)[source]

Return a list of namespace‘s names and their values, in the format ‘name -> value’

localfile(*args)[source]

Create a new filename relative to the invoked program path. args can be a list, which is concatenated onto the end of the path using os.path.join()

log_test(message='', stream=None)[source]

Using testname(), writes the invoking method into the log, adding message if given.

If stream is given, also writes the message to the stream.

Typical invocation would be as the first line of a test method. Special case for testing this method allows ‘return’ to return the actual string for comparison.

new_test_filename(suffix='', unique=False)[source]

Return a new filename based on the invoking method. If unique is true, invoke os.tempnam() to guarantee a unique name.

static removeDir(path)[source]

Clean up and remove a directory if it exists. Typically used in setUp().

targetfile(*args)[source]

Return a new path in the build test target directory. args can be a list, which is concatenated onto the end of the path using os.path.join()

test_file_diff(desired, produced, desiredIs='file', prefix=None, msg=None)[source]

Perform a unittest assertion that two files match. if desiredIs =’string’, compare desired as a string against the produced file.

If prefix is not None, then replace file paths in desired that contain prefix with the current path of the test directory. The use of prefix is essential for different working copies of code that generates absolute pathnames.

Report appropriate errors if either desired or produced do not exist. Put msg in the assertion if given.

testname(suffix='', stack=1)[source]

Return the name of the method that invoked this method, so that it can be used in constructing filenames, etc. Add the suffix if given. The stack parameter allows other routines to build testnames based on the invoking method, as for example new_test_filename() below

static valueof_arg(arglist, flag, unary=False)[source]

Return the value of a parameter flag or just whether it exists if unary =True

TestSupport.run_test(args, terms)[source]

Return True if we want to run it, False otherwise.

TestSupport.test_block(args, module, keys, userexception=<class 'labtools.labexceptions.LabtoolsWarning'>, stdout=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Abstract the standard testing unit so I can build a help menu for unit_test.py