The oslotest.base Module

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.

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.

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.

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.