The oslotest.base Module

Common utilities used in testing

class oslotest.base.BaseTestCase(*args, **kwds)

Bases: testtools.testcase.TestCase

Base class for unit test classes.

If the environment variable OS_TEST_TIMEOUT is set to an integer value, a timer is configured to control how long individual test cases can run. This lets tests fail for taking too long, and prevents deadlocks from completely hanging test runs.

The class variable DEFAULT_TIMEOUT can be set to configure a test suite default test value for cases in which OS_TEST_TIMEOUT is not set. It defaults to 0, which means no timeout.

The class variable TIMEOUT_SCALING_FACTOR can be set on an individual test class for tests that reasonably take longer than the rest of the test suite so that the overall timeout can be kept small. It defaults to 1.

If the environment variable OS_STDOUT_CAPTURE is set, a fake stream replaces sys.stdout so the test can look at the output it produces.

If the environment variable OS_STDERR_CAPTURE is set, a fake stream replaces sys.stderr so the test can look at the output it produces.

If the environment variable OS_DEBUG is set to a true value, debug logging is enabled. Alternatively, the OS_DEBUG environment variable can be set to a valid log level.

If the environment variable OS_LOG_CAPTURE is set to a true value, a logging fixture is installed to capture the log output.

Uses the fixtures module to configure a NestedTempFile to ensure that all temporary files are created in an isolated location.

Uses the fixtures module to configure a TempHomeDir to change the HOME environment variable to point to a temporary location.

PLEASE NOTE: Usage of this class may change the log level globally by setting the environment variable OS_DEBUG. A mock of time.time will be called many more times than might be expected because it’s called often by the logging module. A usage of such a mock should be avoided when a test needs to verify logging behavior or counts the number of invocations. A workaround is to overload the _fake_logs function in a base class but this will deactivate fake logging globally.

DEFAULT_TIMEOUT = 0
TIMEOUT_SCALING_FACTOR = 1
addCleanup(function, *args, **kwargs)

Add a cleanup function to be called after tearDown.

Functions added with addCleanup will be called in reverse order of adding after tearDown, or after setUp if setUp raises an exception.

If a function added with addCleanup raises an exception, the error will be recorded as a test error, and the next cleanup will then be run.

Cleanup functions are always called before a test finishes running, even if setUp is aborted by an exception.

create_tempfiles(files, ext='.conf', default_encoding='utf-8')

Safely create temporary files.

Parameters
  • files (list of tuple) – Sequence of tuples containing (filename, file_contents).

  • ext (str) – File name extension for the temporary file.

  • default_encoding (str) – Default file content encoding when it is not provided, used to decode the tempfile contents from a text string into a binary string.

Returns

A list of str with the names of the files created.

setUp()

Hook method for setting up the test fixture before exercising it.