API¶
oslotest.base¶
Common utilities used in testing
-
class
oslotest.base.
BaseTestCase
(*args, **kwds)¶ 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 replacessys.stdout
so the test can look at the output it produces.If the environment variable
OS_STDERR_CAPTURE
is set, a fake stream replacessys.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, theOS_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 theHOME
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 oftime.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.-
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.
-
oslotest.createfile¶
-
class
oslotest.createfile.
CreateFileWithContent
(filename, contents, ext='.conf', encoding='utf-8')¶ 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.-
path
¶ The canonical name of the file created.
Parameters: - filename – Base file name or full literal path to the file to be created.
- contents – The data to write to the file. Unicode data will be encoded before being written.
- ext – An extension to add to filename.
- encoding – An encoding to use for unicode data (ignored for byte strings).
-
oslotest.log¶
-
class
oslotest.log.
ConfigureLogging
(format='%(levelname)8s [%(name)s] %(message)s')¶ Configure logging.
The behavior is managed through two environment variables. If
OS_DEBUG
is true then the logging level is set to debug. IfOS_LOG_CAPTURE
is true a FakeLogger is configured. Alternatively,OS_DEBUG
can be set to an explicit log level, such asINFO
.“True” values include
True
,true
,1
andyes
. Valid log levels includeDEBUG
,INFO
,WARNING
,ERROR
,TRACE
andCRITICAL
(or any other valid integer logging level).-
logger
¶ The logger fixture, if it is created.
-
level
¶ logging.DEBUG
if debug logging is enabled, otherwise the log level specified byOS_DEBUG
, otherwiseNone
.
Parameters: format – The logging format string to use. -
DEFAULT_FORMAT
= '%(levelname)8s [%(name)s] %(message)s'¶ Default log format
-
oslotest.mockpatch¶
-
class
oslotest.mockpatch.
Multiple
(obj, **kwargs)¶ 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: - obj (str or object) – Object or name containing values being mocked.
- kwargs – names and values of attributes of obj to be mocked.
-
mock
¶ The mock.
-
DEFAULT
= sentinel.DEFAULT¶ Triggers a MagicMock to be created for a named attribute.
oslotest.moxstubout¶
-
class
oslotest.moxstubout.
MoxStubout
¶ Deal with code around mox and stubout as a fixture.
oslotest.output¶
-
class
oslotest.output.
CaptureOutput
¶ Optionally capture the output streams.
The behavior is managed through two environment variables. If
OS_STDOUT_CAPTURE
is true then stdout is captured and ifOS_STDERR_CAPTURE
is true then stderr is captured.“True” values include
True
,true
,1
, andyes
.-
stdout
¶ The
stream
attribute from aStringStream
instance replacing stdout.
-
stderr
¶ The
stream
attribute from aStringStream
instance replacing stderr.
-