testing_utils.py

Define utils for use in testing.

class openmdao.utils.testing_utils.MissingImports(missing_imports)[source]

Bases: object

ContextManager that emulates missing python packages or modules.

Each import is checked to see if it starts with a missing import.

For instance:

>>> with MissingImports('matplotlib'):
>>>    from matplotlib.pyplot import plt

will fail because ‘matplotlib.pyplot’.startswith(‘matplotlib’) is True.

This implementation modifies builtins.__import__ which is allowed but highly discouraged according to the documentation, but implementing a MetaPathFinder seemed like overkill. Use at your own risk.

Parameters
missing_importsstr or Sequence of str

A string or sequence of strings that denotes modules that should appear to be absent for testing purposes.

Attributes
missing_importsstr or Sequence of str

A string or sequence of strings that denotes modules that should appear to be absent for testing purposes.

_cached_importNone or builtin

A cached import to emulate the missing import

__init__(missing_imports)[source]

Initialize attributes.

openmdao.utils.testing_utils.require_pyoptsparse(optimizer=None)[source]

Decorate test to raise a skiptest if a required pyoptsparse optimizer cannot be imported.

Parameters
optimizerstr

Pyoptsparse optimizer string. Default is None, which just checks for pyoptsparse.

Returns
TestCase or TestCase.method

The decorated TestCase class or method.

openmdao.utils.testing_utils.use_tempdirs(cls)[source]

Decorate each test in a unittest.TestCase so it runs in its own directory.

TestCase methods setUp and tearDown are replaced with _new_setup and _new_teardown, above. Method _new_setup creates a temporary directory in which to run the test, stores it in self.tempdir, and then calls the original setUp method. Method _new_teardown first runs the original tearDown method, and then returns to the original starting directory and deletes the temporary directory.

Returns
TestCase

The decorated TestCase class.

Attributes
clsTestCase

TestCase being decorated to use a tempdir for each test.