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 pltwill 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.force_check_partials(prob, *args, **kwargs)[source]
Force the checking of partials even for components with _no_check_partials set.
- Parameters:
- probProblem
The Problem being checked.
- *argslist
Positional args.
- **kwargsdict
Keyword args.
- Returns:
- dict
Total derivative comparison data.
- openmdao.utils.testing_utils.parameterized_name(testcase_func, num, param)[source]
Generate a name for a parameterized test from the parameters.
- Parameters:
- testcase_funcstr
the root test function name
- numint
parameter number
- paramany
parameter value
- Returns:
- TestCase or TestCase.method
The decorated TestCase class or method.
- 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.
- class openmdao.utils.testing_utils.set_env_vars(**envs)[source]
Bases:
object
Decorate a function to temporarily set some environment variables.
- Parameters:
- **envsdict
Keyword args corresponding to environment variables to set.
- Attributes:
- envsdict
Saved mapping of environment var name to value.
- __init__(**envs)[source]
Initialize attributes.
- openmdao.utils.testing_utils.set_env_vars_context(**kwargs)[source]
Context to temporarily set some environment variables.
- Parameters:
- **kwargsdict
Keyword args corresponding to environment variables to set.
- Yields:
- None
- 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.