Common utilities used in testing
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.
Safely create temporary files.
Parameters: |
|
---|---|
Returns: | A list of str with the names of the files created. |
Create a temporary file with the given content.
Creates a file using a predictable name, to be used by tests for code that need a filename to load data or otherwise interact with the real filesystem.
Warning
It is the responsibility of the caller to ensure that the file is removed.
Users of this fixture may also want to use fixtures.NestedTempfile to set the temporary directory somewhere safe and to ensure the files are cleaned up.
The canonical name of the file created.
Parameters: |
|
---|
Configure logging.
The behavior is managed through two environment variables. If OS_DEBUG is true then the logging level is set to debug. If OS_LOG_CAPTURE is true a FakeLogger is configured. Alternatively, OS_DEBUG can be set to an explicit log level, such as INFO.
“True” values include True, true, 1 and yes. Valid log levels include DEBUG, INFO, WARNING, ERROR, TRACE and CRITICAL (or any other valid integer logging level).
The logger fixture, if it is created.
logging.DEBUG if debug logging is enabled, otherwise the log level specified by OS_DEBUG, otherwise None.
Parameters: | format – The logging format string to use. |
---|
Default log format
Deal with code around mock.patch.multiple.
Pass name=value to replace obj.name with value.
Pass name= Multiple.DEFAULT to replace obj.name with a MagicMock instance.
Parameters: |
|
---|
The mock.
Triggers a MagicMock to be created for a named attribute.
Deal with code around mox and stubout as a fixture.
Optionally capture the output streams.
The behavior is managed through two environment variables. If OS_STDOUT_CAPTURE is true then stdout is captured and if OS_STDERR_CAPTURE is true then stderr is captured.
“True” values include True, true, 1, and yes.
The stream attribute from a StringStream instance replacing stdout.
The stream attribute from a StringStream instance replacing stderr.