Waelstow

[wel stoʊ] / noun

  1. literal: “death field” or “slaugher field”;
  2. Anglo Saxon term for battlefields
  3. A collection of Python testing utilties

Installation

$ pip install waelstow

Supports

Tested with Python 3.7 - Python 3.11

Methods

waelstow.capture_stderr()

This Context Manager redirects STDERR to a StringIO objects which is returned from the Context. On exit STDERR is restored.

Example:

with capture_stderr() as capture:
    print('foo')

# got here? => capture.getvalue() will now have "foo\n"
waelstow.capture_stdout()

This Context Manager redirects STDOUT to a StringIO objects which is returned from the Context. On exit STDOUT is restored.

Example:

with capture_stdout() as capture:
    print('foo')

# got here? => capture.getvalue() will now have "foo\n"
waelstow.discover_tests(start_dir, labels=[], pattern='test*.py')

Discovers tests in a given module filtered by labels. Supports short-cut labels as defined in find_shortcut_labels().

Parameters:
  • start_dir – Name of directory to begin looking in
  • labels – Optional list of labels to filter the tests by
Returns:

TestSuite with tests

waelstow.find_shortcut_tests(suites, shortcut_labels)

Takes a suite of tests and returns a list of tests that conform to the passed in list of short-cut labels. A short-cut label begins with a “=” and indicates a partial string contained in either the name of the test or the test class.

Example:

Parameters:
  • suites – A single or iterable of TestSuite objects to create the test subset from
  • shortcut_labels – A list of short-cut labels to use to cull the list
Returns:

A list of TestCase objects

waelstow.list_tests(suites)

Takes a list of suites and returns an iterable of all the tests in the suites and their children.

Parameters:suites – A single or an interable of TestSuite objects whose contents will be listed
Returns:Iterable of TestCase objects contained within the suites
class waelstow.noted_raise(message)

This Context Manager is used to annotate an exception raised within its block. Sometimes when testing you might have a loop with variables, if an assert fails in the loop you’d like to see more of the context. This context manager allows you to define a message format based on local variables and if there is an exception it will add info the exception’s message.

If you’re using a version of Python that supports Exception.add_note() (Python >=3.11), your formatted message is added as a note. If you are not, it assumes the first argument to the Exception is the message and appends text to it.

Parameters:message – The message string to append in the case of an exception. This string is passed to str.format() using local variables as the context. Anything local variable you have defined will be available as a named value in the message string.
with noted_raise("Counter:{counter}"):
    for counter in range(1, 10):
        self.assertTrue(counter < 5)

The above example will result in AssertionEror with either a note that containing ” Counter:5”, or the exceptions message string having the same appended to the end.

__init__(message)

Initialize self. See help(type(self)) for accurate signature.

waelstow.pprint(data)

Alternative to pprint.PrettyPrinter() that uses json.dumps() for sorting and displaying data.

Parameters:data – item to print to STDOUT. The item must be json serializable!
waelstow.replaced_directory(dirname)

This Context Manager is used to move the contents of a directory elsewhere temporarily and put them back upon exit. This allows testing code to use the same file directories as normal code without fear of damage.

The name of the temporary directory which contains your files is yielded.

Parameters:dirname – Path name of the directory to be replaced.

Example:

with replaced_directory('/foo/bar/') as rd:
    # "/foo/bar/" has been moved & renamed
    with open('/foo/bar/thing.txt', 'w') as f:
        f.write('stuff')
        f.close()


# got here? => "/foo/bar/ is now restored and temp has been wiped, 
# "thing.txt" is gone

Indices and tables