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.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.rel_num_diff(n1, n2)[source]

Return the relative numerical difference between two numbers.

Parameters:
n1float

First number to compare.

n2float

Second number to compare.

Returns:
float

Relative difference between the numbers.

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.snum_equal(s1, s2, atol=1e-06, rtol=1e-06)[source]

Compare two strings, and if they contain numbers, compare the numbers subject to tolerance.

Also compare the non-number parts of the strings exactly.

Parameters:
s1str

First string to compare.

s2str

Second string to compare.

atolfloat, optional

Absolute tolerance. The default is 1e-6.

rtolfloat, optional

Relative tolerance. The default is 1e-6.

Returns:
bool

True if the strings are equal within the tolerance, False otherwise.

openmdao.utils.testing_utils.snum_iter(s)[source]

Iterate through a string, yielding numeric strings as numbers along with non-numeric strings.

Parameters:
sstr

The string to iterate through.

Yields:
str

The next number or non-number.

bool

True if the string is a number, False otherwise.

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.