oslotest(1)

OSLOTEST(1) oslotest OSLOTEST(1)

NAME

oslotest - oslotest 4.5.0

CONTENTS

Installation

At the command line:

$ pip install oslotest


Contributing

If you would like to contribute to the development of oslo's libraries, first you must take a look to this page:

https://specs.openstack.org/openstack/oslo-specs/specs/policy/contributing.html


If you would like to contribute to the development of OpenStack, you must follow the steps in this page:

http://docs.openstack.org/infra/manual/developers.html


Once those steps have been completed, changes to OpenStack should be submitted for review via the Gerrit tool, following the workflow documented at:

http://docs.openstack.org/infra/manual/developers.html#development-workflow


Pull requests submitted through GitHub will be ignored.

Bugs should be filed on Launchpad, not GitHub:

https://bugs.launchpad.net/oslotest


Using oslotest

Features

Debugging with oslo_debug_helper

The oslotest package also distributes a shell file that may be used to assist in debugging python code. The shell file uses testtools, and supports debugging with pdb.

Adding breakpoints to the code

The typical usage to break into the debugger from a running program is to insert:

import pdb; pdb.set_trace()


Update tox.ini

Within the tox.ini file of your project add the following:

[testenv:debug]
commands = oslo_debug_helper {posargs}


If the project name, and the module that precedes the tests directory do not match, then consider passing a -t argument to oslo_debug_helper to indicate the directory where tests are located.

For example, the python-keystoneclient project has tests in keystoneclient/tests, thus it would have to pass in:

[testenv:debug]
commands = oslo_debug_helper -t keystoneclient/tests {posargs}


Similarly, most oslo projects have the tests at the package level, it would have to pass in:

[testenv:debug]
commands = oslo_debug_helper -t tests {posargs}


Debugging with tox

To enable debugging, run tox with the debug environment:

$ tox -e debug
$ tox -e debug test_notifications
$ tox -e debug test_notifications.NotificationsTestCase
$ tox -e debug test_notifications.NotificationsTestCase.test_send_notification


Which results in an interactive prompt:

> /opt/stack/ceilometer/ceilometer/tests/identity/test_notifications.py(106)notification_for_role_change()
-> action_name = '%s.%s' % (action, ROLE_ASSIGNMENT)
(Pdb)


Debugging Tests

Running tests through testrepository makes it difficult to use pdb for debugging them. oslotest includes oslo_debug_helper to make using pdb simpler/possible.

First, add a pdb call to the test code:

import pdb; pdb.set_trace()


Then run the tests through oslo_debug_helper like

$ oslo_debug_helper [tests to run]


or

$ tox -e venv -- oslo_debug_helper [tests to run]


SEE ALSO:

https://wiki.openstack.org/wiki/Testr



Testing

Cross-testing With Other Projects

The oslotest package can be cross-tested against its consuming projects to ensure that no changes to the library break the tests in those other projects.

In the Gate

Refer to the instructions in https://wiki.openstack.org/wiki/Oslo/UsingALibrary for setting up cross-test jobs in the gate.

Locally

To run the cross-tests locally, invoke the script directly, passing the path to the other source repository and the tox environment name to use:

$ cd oslo.test
$ ./tools/oslo_run_cross_tests ~/repos/openstack/oslo.config py27


Mock autospec

In typical unit tests, almost all of the dependencies are mocked or patched (mock.patch), without any guarantee that the mocked methods actually exist, or if their signatures are respected. Because of this, actual issues can be easily overlooked and missed, as the unit tests are wrongfully passing.

The mock.Mock class accepts a spec as an argument, which only solves half the problem: it only checks if an attribute exists, based on the given spec. It does not guarantee that the given attribute is actually a method, or if its signature is respected. The Mock class does not accept an autospec argument [1].

mock.patch, mock.patch.object, mock.patch.multiple accept an autospec argument, but because of a bug [2], it cannot be used properly.

oslotest offers a solution for problems mentioned above.

1.
https://github.com/testing-cabal/mock/issues/393
2.
https://github.com/testing-cabal/mock/issues/396

Patching the mock module

The oslotest.mock_fixture module contains 2 components:

  • patch_mock_module
  • MockAutospecFixture

Both components need to be used in order to fix various issues within mock regarding autospec.

patch_mock_module

At the moment, mock.patch, mock.patch.object, mock.patch.multiple accepts the autospec argument, but it does not correctly consume the self / cls argument of methods or class methods.

patch_mock_module addresses this issue. In order to make sure that the original version of mock.patch is not used by the unit tests, this function has to be called as early as possible within the test module, or the base test module. E.g.:

nova/test.py
...
from oslotest import mock_fixture
mock_fixture.patch_mock_module()


Additionally, this function will set the autospec argument's value to True, unless otherwise specified or these arguments are passed in: new_callable, create, spec.

MockAutospecFixture

mock.Mock and mock.MagicMock classes do not accept any autospec argument. This fixture will replace the mock.Mock and mock.MagicMock classes with subclasses which accepts the said argument.

The fixture can be used in the test setUp method. E.g.:

nova/test.py
...
from oslotest import mock_fixture
class TestCase(testtools.TestCase):

def setUp(self):
super(TestCase, self).setUp()
self.useFixture(mock_fixture.MockAutospecFixture())


Mock autospec usage

Consider the following class as an example:

class Foo(object):

def bar(self, a, b, c, d=None):
pass


Using the setup described above, the following unit tests will now pass correctly:

class FooTestCase(TestCase):

def test_mock_bar(self):
mock_foo = mock.Mock(autospec=Foo)
self.assertRaises(TypeError, mock_foo.bar, invalid='argument')
@mock.patch.object(Foo, 'bar', autospec=True)
def test_patch_bar(self, mock_bar):
foo = Foo()
self.assertRaises(TypeError, foo.bar, invalid='argument')


Cross-project Unit Testing

Libraries in OpenStack have an unusual ability to introduce breaking changes. All of the projects are run together from source in one form or another during the integration tests, but they are not combined from source when unit tests are run. The server applications do not generally import code from other projects, so their unit tests are isolated. The libraries, however, are fundamentally intended to be used during unit tests as well as integration tests. Testing the full cross-product of libraries and consuming projects would consume all available test servers, and so we cannot run all of the tests on all patches to the libraries. As an alternative, we have a few scripts in oslotest for running the unit tests in serial. The result takes far too long (usually overnight) to run in the OpenStack infrastructure. Instead, they are usually run by hand on a dedicated system. A cloud VM works well for this purpose, especially considering how much of it is now automated.

Check Out OpenStack Source

The first step for all of the cross-project unit tests tools is to ensure that you have a full copy of the OpenStack source checked out. You can do this yourself through gerrit's ssh API, or you can use the clone_openstack.sh command in the tools directory of the openstack/oslo-incubator repository.

For example:

$ mkdir -p ~/repos/openstack
$ cd ~/repos/openstack
$ git clone https://opendev.org/openstack/oslo-incubator
$ cd ~/repos
$ ./openstack/oslo-incubator/tools/clone_openstack.sh


The first time the script runs it will take quite some time, since it has to download the entire history of every OpenStack project.

Testing One Project

oslo_run_cross_tests runs one set of unit tests for one library against all of the consuming projects. It should be run from the directory with the library to be tested, and passed arguments telling it about another project whose tests should be run.

For example, to run the py27 test suite from nova using the currently checked out sources for oslo.config, run:

$ cd ~/repos/openstack/oslo.config
$ tox -e venv -- oslo_run_cross_tests ~/repos/openstack/nova py27


Testing All Consumers

oslo_run_pre_release_tests builds on oslo_run_cross_tests to find all of the consuming projects and run their tests automatically. The pre-release script needs to know where the source has been checked out, so the first step is to create its configuration file.

Edit ~/.oslo.conf to contain:

[DEFAULT]
repo_root = /path/to/repos


Replace /path/to/repos with the full, expanded, absolute path to the location where the source code was checked out. For example, if you followed the instructions above using clone_openstack.sh in ~/repos and your user name is theuser the path would be /home/theuser/repos.

Returning to the earlier example, to test oslo.config with all of the projects that use it, go to the oslo.config source directory and run oslo_run_pre_release_tests.

$ cd ~/repos/openstack/oslo.config
$ tox -e venv -- oslo_run_pre_release_tests


The output for each test set is logged to a separate file in the current directory, to make them easy to examine.

Use the --update or -u option to force a git pull for each consuming projects before running its tests (useful for maintaining a long-running system to host these tests).

Use the --verbose or -v option to report more verbosely about what is happening, including the number of projects being tested.

Use --env or -e to add a tox environment to test. By default the py27 and pep8 environments are used because those have been shown to provide a good balance between finding problems and running the tests quickly.

Use the --ref option to test a specific commit reference of the library under test. The default is to leave the source directory untouched, but if this option is specified git checkout is used to force the source tree to the specified reference.

Other Useful Resources

  • Mock library documentation
  • OpenStack Bootstrapping Hour: Mock Best Practices: video and etherpad

CHANGES

4.5.0

  • setup.cfg: Replace dashes with underscores
  • Use TOX_CONSTRAINTS_FILE
  • Document unit of measure for OS_TEST_TIMEOUT
  • Move flake8 as a pre-commit local target
  • Remove lower-constraints remnants
  • Dropping lower constraints testing
  • Use TOX_CONSTRAINTS_FILE
  • Use py3 as the default runtime for tox
  • Adding pre-commit
  • Add Python3 wallaby unit tests
  • Update master for stable/victoria

4.4.1

4.4.0

4.3.0

  • Stop to use the __future__ module
  • Switch to newer openstackdocstheme and reno versions
  • Align contributing doc with oslo's policy
  • Bump default tox env from py37 to py38
  • Add py38 package metadata
  • Add release notes links to doc index
  • Add Python3 victoria unit tests
  • Update master for stable/ussuri
  • Revert "Revert "Switch to unittest.mock from mock""

4.2.0

Revert "Switch to unittest.mock from mock"

4.1.0

  • Switch to unittest.mock from mock
  • Remove os-client-config and debtcollector

4.0.0

  • Remove 'oslotest.functional'
  • Remove 'oslotest.moxstubout' module
  • remove outdated header
  • gitignore: Ignore reno artefacts
  • [ussuri][goal] Drop python 2.7 support and testing
  • trivial: Cleanup of doc config file
  • tox: Trivial cleanup
  • tools: Default to Python 3

3.9.0

  • Invoke correct python version in shell scripts
  • Switch to Ussuri jobs
  • Update master for stable/train

3.8.1

Add Python 3 Train unit tests

3.8.0

  • Sync Sphinx requirement
  • Replace git.openstack.org URLs with opendev.org URLs
  • Stop testing mock functionality
  • OpenDev Migration Patch
  • Dropping the py35 testing
  • Replace openstack.org git:// URLs with https://
  • Update master for stable/stein

3.7.1

  • Change python3.5 job to python3.7 job on Stein+
  • Update hacking version
  • Change openstack-dev to openstack-discuss
  • Title underline too long
  • Remove stestr from requirements.txt
  • Don't quote {posargs} in tox.ini
  • Fix nits in timeout change

3.7.0

  • Add DEFAULT_TIMEOUT and TIMEOUT_SCALING_FACTOR
  • Use templates for cover and lower-constraints
  • Fix requirements check ci in oslotest
  • add python 3.6 unit test job
  • import zuul job settings from project-config
  • Update reno for stable/rocky
  • Add release note link in README

3.6.0

  • Switch to using stestr
  • fix tox python3 overrides

3.5.0

  • Deprecate MoxStubout class
  • Fix requirements
  • mock: Perform patch's autospec checks on __enter__

3.4.2

  • Trivial: Update pypi url to new url
  • set default python to python3
  • mock: Apply autospec to a mock's return_value

3.4.1

mock: Properly patch mock.MagicMock

3.4.0

  • add lower-constraints job
  • make the CaptureOutput fixture easier to control
  • Updated from global requirements

3.3.0

  • Updated from global requirements
  • Update links in README
  • Update reno for stable/queens
  • Updated from global requirements
  • Updated from global requirements
  • mock: Fixes mock.patch.multiple autospec

3.2.0

Adds mock autospec fixture

3.1.0

  • Remove -U from pip install
  • Avoid tox_install.sh for constraints support
  • Updated from global requirements
  • Remove setting of version/release from releasenotes
  • Updated from global requirements

3.0.0

2.18.1

2.18.0

  • Updated from global requirements
  • Updated from global requirements
  • Deprecate oslotest.functional
  • Remove oslotest.mockpatch
  • Updated from global requirements
  • Updated from global requirements
  • Update reno for stable/pike
  • Updated from global requirements

2.17.0

  • Update URLs in documents according to document migration
  • Use assertIsNone(...) instead of assertIs(None,...)
  • rearrange content to fit the new standard layout
  • Using fixtures instead of deprecated mockpatch module

2.16.1

Switch from oslosphinx to openstackdocstheme

2.16.0

  • Updated from global requirements
  • Trivial fix style in document
  • Updated from global requirements
  • Remove pbr warnerrors in favor of sphinx check
  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements

2.15.0

  • Updated from global requirements
  • Remove support for py34

2.14.0

  • Updated from global requirements
  • [Fix gate]Update test requirement
  • Updated from global requirements
  • Update reno for stable/ocata
  • Updated from global requirements

2.13.0

  • Add Constraints support
  • Show team and repo badges on README

2.12.0

  • Updated from global requirements
  • Add reno for release notes management
  • Updated from global requirements
  • Updated from global requirements
  • Changed the home-page link

2.11.0

  • Remove testscenarios from requirements
  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements

2.10.0

Updated from global requirements

2.9.0

Updated from global requirements

2.8.0

  • Import mock so that it works on Python 3.x
  • Fix parameters of assertEqual are misplaced
  • Updated from global requirements
  • Add Python 3.5 classifier and venv
  • A DisableModules fixture that removes modules from path

2.7.0

Updated from global requirements

2.6.0

Updated from global requirements

2.5.0

  • Remove mockpatch re-implementations
  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements

2.3.0

Add some gitignore files

2.2.0

  • move unit tests into the oslotest package
  • Updated from global requirements
  • Hack to get back stopall cleanup behavior feature
  • Fix misspelling

2.1.0

  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements
  • Trival: Remove 'MANIFEST.in'

2.0.0

  • Updated from global requirements
  • Remove Python 2.6 classifier
  • mockpatch: deprecate in favor of native fixtures
  • Remove python 2.6 and cleanup tox.ini
  • Updated from global requirements

1.12.0

  • Fix coverage configuration and execution
  • Updated from global requirements
  • Add documentation about using oslo_debug_helper
  • add oslo.config a test requirement
  • clean up readme and doc title
  • clean up toctree
  • auto-generate API documentation
  • Fix the home-page with Oslotest wikipage
  • Fixup docstrings
  • Updated from global requirements

1.11.0

Updated from global requirements

1.10.0

  • Allow TRACE and integer logging levels for 'OS_DEBUG'
  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements

1.9.0

  • Fix use of mock
  • Updated from global requirements
  • Add documentation for cross-project unit testing
  • Updated from global requirements
  • Fix build break with Fixtures 1.3
  • Updated from global requirements

1.8.0

  • Updated from global requirements
  • Allow ``OS_DEBUG`` environment variable to specify log level
  • Updated from global requirements
  • always rebuild cross-test venv
  • Add CreateFileWithContent fixture
  • Create ConfigureLogging fixture
  • Create CaptureOutput fixture
  • Create Timeout wrapper fixture
  • Document the mock attribute for mockpatch

1.7.0

  • Updated from global requirements
  • Fix argument handling in oslo_run_cross_tests
  • Add class to deal with clouds.yaml support
  • Remove unneeded runtime pbr dep
  • Updated from global requirements
  • Advertise support for Python3.4 / Remove support for Python 3.3
  • Do not sync run_cross_tests.sh
  • Remove unused discover dependency

1.6.0

  • Uncap library requirements for liberty
  • Cleanup README.rst and setup.cfg
  • mockpatch: factorize code
  • Update to latest hacking
  • Updated from global requirements
  • mockpatch: fix a potential race condition

1.5.1

1.5.0

  • Force rebuild egg-info before running cross tests
  • Restore missing module for pre-release test script
  • Updated from global requirements

1.4.0

  • Set a higher default for maxDiff
  • Move the script for running pre-releases into oslotest
  • Update docs for new script name
  • Publish cross-test runner as part of oslotest
  • Remove six.moves call
  • Fix for mktemp failure on osx
  • Activate pep8 check that _ is imported
  • Workflow documentation is now in infra-manual
  • Fix the URL for reporting bugs in the README

1.3.0

  • Updated from global requirements
  • Updated from global requirements
  • Updated from global requirements
  • Add pbr to installation requirements
  • Clean up the docs for oslo_debug_helper

1.2.0

  • Updated from global requirements
  • Set install_command in tox to avoid pre-releases
  • Add an extra parameter for test directory in debugger script
  • Handle tempfile content encoding
  • Work toward Python 3.4 support and testing
  • Add links to best practices video and etherpad
  • Updated from global requirements
  • Drop .sh extension from oslo_debug_helper.sh
  • Add history/changelog to docs
  • fix typo and formatting in contributing docs

1.1.0

  • warn against sorting requirements
  • Add fixture for mock.patch.multiple
  • Ensure that mock.patch.stopall is called last
  • Remove differences between Python 2.x and 3.x versions
  • Require six
  • Add documentation for running oslo_debug_heler.sh
  • Restructure oslotest docs
  • Add pdb support to tox with debug helper shell script
  • Updated from global requirements
  • Cleaning up index.rst file
  • Add known issue about time.time mocking
  • Updated from global requirements
  • Add API documentation
  • Moving to use the mock module found in Python3

1.1.0.0a1

  • Update to hacking 0.9.2
  • Cleanup mock patches on BaseTestCase tearDown()
  • Add unit test for olsotest base class
  • fix .gitreview after rename
  • Sync new sphinx requirement spec
  • Set log level to default value
  • Updated from global requirements
  • Update cross-test directions
  • Update project name in doc build

1.0.0

  • Import run_cross_tests.sh from oslo-incubator
  • Fix up documentation files
  • Fake logger as instance attribute
  • Require testrepository and other tools at runtime
  • Updated from global requirements
  • Add tool to run cross-project tests

0.1

  • Rename oslo.test to oslotest
  • Add test for moxstubout
  • Switch to oslosphinx
  • Sync requirements and fix pep8 errors
  • Remove oslo.test.fixture
  • Remove unused tempdirs attribute
  • Remove translations infrastructure
  • apply oslo-cookiecutter
  • Differentiate runtime and test requirements
  • flatten package hierarchy
  • remove dependency on oslo.config to avoid cycle
  • Remove lockutils fixture from this library
  • Run python 3.3 tests first to set testr db type
  • Add packaging and test control files
  • rearrange files into the proper package
  • Generalize base test case into common code
  • Add 'new' parameter to mock.Patch and mock.PatchObject classes
  • Make the log capture in tests more configurable
  • log all test messages not just oslo ones
  • Fix violations of H302:import only modules
  • Trivial: Make vertical white space after license header consistent
  • Remove vim header
  • Fix copyright header on test module
  • Use cleaner version from cookiecutter OpenStack template
  • Add TempHomeDir fixture which is already part of cookiecutter template
  • Fix typos in oslo
  • Move LockFixture into a fixtures module
  • Consolidate the use of stubs
  • Make openstack.common.fixture.config Py3 compliant
  • Using NestedTempfile in new BaseTestCase class
  • Bump hacking to 0.7.0
  • Add a fixture for dealing with config
  • Add common part of test-related tools to oslo
  • Add eclipse project files to .gitignore
  • Add .testrepository to .gitignore
  • Ignore backup files in .gitignore
  • Add join_consumer_pool() to RPC connections
  • Add a fixture for dealing with mock patching
  • Start adding reusable test fixtures
  • updating sphinx documentation
  • Correcting openstack-common mv to oslo-incubator
  • Update .gitreview for oslo
  • .gitignore updates for generated files
  • Updated tox config for multi-python testing
  • Added .gitreview file
  • ignore cover's html directory
  • Rajaram/Vinkesh|increased tests for Request and Response serializers
  • Rajaram/Vinkesh|Added nova's serializaiton classes into common
  • Initial skeleton project

API

oslotest

oslotest package

Subpackages

oslotest.tools package

Submodules

oslotest.tools.config module

Utilities functions for working with oslo.config from the tool scripts.

oslotest.tools.config.get_config_parser()

oslotest.tools.config.parse_arguments(conf)

Module contents

Submodules

oslotest.base module

Common utilities used in testing

class oslotest.base.BaseTestCase(*args, **kwds)
Bases: TestCase

Base class for unit test classes.

If the environment variable OS_TEST_TIMEOUT is set to an integer value (seconds), 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.


oslotest.createfile module

class oslotest.createfile.CreateFileWithContent(filename, contents, ext='.conf', encoding='utf-8')
Bases: Fixture

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).


setUp()
Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
Returns
None.
Changed in 1.3
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
Changed in 1.3.1
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.



oslotest.log module

class oslotest.log.ConfigureLogging(format='%(levelname)8s [%(name)s] %(message)s')
Bases: Fixture

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).

logger
The logger fixture, if it is created.

level
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_FORMAT = '%(levelname)8s [%(name)s] %(message)s'
Default log format

setUp()
Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
Returns
None.
Changed in 1.3
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
Changed in 1.3.1
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.



oslotest.mock_fixture module

class oslotest.mock_fixture.MockAutospecFixture
Bases: Fixture

A fixture to add / fix the autospec feature into the mock library.

The current version of the mock library has a few unaddressed issues, which can lead to erroneous unit tests, and can hide actual issues. This fixture is to be removed once these issues have been addressed in the mock library.

Issue addressed by the fixture:

mocked method's signature checking:
  • https://github.com/testing-cabal/mock/issues/393
  • mock can only accept a spec object / class, and it makes sure that that attribute exists, but it does not check whether the given attribute is callable, or if its signature is respected in any way.
  • adds autospec argument. If the autospec argument is given, the mocked method's signature is also checked.



setUp()
Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
Returns
None.
Changed in 1.3
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
Changed in 1.3.1
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.



oslotest.mock_fixture.patch_mock_module()
Replaces the mock.patch class.

oslotest.modules module

class oslotest.modules.DisableModuleFixture(module, *args, **kwargs)
Bases: Fixture

A fixture to provide support for unloading/disabling modules.

setUp()
Ensure ImportError for the specified module.


oslotest.output module

class oslotest.output.CaptureOutput(do_stdout=None, do_stderr=None)
Bases: Fixture

Optionally capture the output streams.

The behavior is managed through arguments to the constructor. The default behavior is controlled via 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.

Parameters
  • do_stdout (bool) -- Whether to capture stdout.
  • do_stderr (bool) -- Whether to capture stderr.


stdout
The stream attribute from a StringStream instance replacing stdout.

stderr
The stream attribute from a StringStream instance replacing stderr.

setUp()
Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
Returns
None.
Changed in 1.3
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
Changed in 1.3.1
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.



oslotest.timeout module

class oslotest.timeout.Timeout(default_timeout=0, scaling_factor=1)
Bases: Fixture

Set the maximum length of time for the test to run.

Uses OS_TEST_TIMEOUT to get the timeout.

setUp()
Prepare the Fixture for use.

This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.

After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).

Raises
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
Returns
None.
Changed in 1.3
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
Changed in 1.3.1
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.



Module contents

RELEASE NOTES

Read also the oslotest Release Notes.

INDICES AND TABLES

  • Index
  • Module Index
  • Search Page

AUTHOR

unknown

COPYRIGHT

2014-2024, OpenStack Foundation

January 26, 2024 4.5.0