tf.test.TestCase

TensorFlow 2 version View source on GitHub

Base class for tests that need to test TensorFlow.

Child Classes

class failureException

Methods

addCleanup

Add a function, with arguments, to be called when the test is completed. Functions added are called on a LIFO basis and are called after tearDown on test failure or success.

Cleanup items are called even if setUp fails (unlike tearDown).

addTypeEqualityFunc

Add a type specific assertEqual style function to compare a type.

This method is for use by TestCase subclasses that need to register their own type equality functions to provide nicer error messages.

Args
typeobj The data type to call this function on when both values are of the same type in assertEqual().
function The callable taking two arguments and an optional msg= argument that raises self.failureException with a useful error message when the two arguments are not equal.

assertAllClose

View source

Asserts that two structures of numpy arrays or Tensors, have near values.

a and b can be arbitrarily nested structures. A layer of a nested structure can be a dict, namedtuple, tuple or list.

Args
a The expected numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor), or any arbitrarily nested of structure of these.
b The actual numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor), or any arbitrarily nested of structure of these.
rtol relative tolerance.
atol absolute tolerance.
msg Optional message to report on failure.

Raises
ValueError if only one of a[p] and b[p] is a dict or a[p] and b[p] have different length, where [p] denotes a path to the nested structure, e.g. given a = [(1, 1), {'d': (6, 7)}] and [p] = [1]['d'], then a[p] = (6, 7).

assertAllCloseAccordingToType

View source

Like assertAllClose, but also suitable for comparing fp16 arrays.

In particular, the tolerance is reduced to 1e-3 if at least one of the arguments is of type float16.

Args
a the expected numpy ndarray or anything can be converted to one.
b the actual numpy ndarray or anything can be converted to one.
rtol relative tolerance.
atol absolute tolerance.
float_rtol relative tolerance for float32.
float_atol absolute tolerance for float32.
half_rtol relative tolerance for float16.
half_atol absolute tolerance for float16.
bfloat16_rtol relative tolerance for bfloat16.
bfloat16_atol absolute tolerance for bfloat16.
msg Optional message to report on failure.

assertAllEqual

View source

Asserts that two numpy arrays or Tensors have the same values.

Args
a the expected numpy ndarray or anything can be converted to one.
b the actual numpy ndarray or anything can be converted to one.
msg Optional message to report on failure.

assertAllGreater

View source

Assert element values are all greater than a target value.

Args
a The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
comparison_target The target value of comparison.

assertAllGreaterEqual

View source

Assert element values are all greater than or equal to a target value.

Args
a The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
comparison_target The target value of comparison.

assertAllInRange

View source

Assert that elements in a Tensor are all in a given range.

Args
target The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
lower_bound lower bound of the range
upper_bound upper bound of the range
open_lower_bound (bool) whether the lower bound is open (i.e., > rather than the default >=)
open_upper_bound (bool) whether the upper bound is open (i.e., < rather than the default <=)

Raises
AssertionError if the value tensor does not have an ordered numeric type (float* or int*), or if there are nan values, or if any of the elements do not fall in the specified range.

assertAllInSet

View source

Assert that elements of a Tensor are all in a given closed set.

Args
target The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
expected_set (list, tuple or set) The closed set that the elements of the value of target are expected to fall into.

Raises
AssertionError if any of the elements do not fall into expected_set.

assertAllLess

View source

Assert element values are all less than a target value.

Args
a The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
comparison_target The target value of comparison.

assertAllLessEqual

View source

Assert element values are all less than or equal to a target value.

Args
a The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
comparison_target The target value of comparison.

assertAlmostEqual

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

If the two objects compare equal then they will automatically compare almost equal.

assertAlmostEquals

assertArrayNear

View source

Asserts that two float arrays are near each other.

Checks that for all elements of farray1 and farray2 |f1 - f2| < err. Asserts a test failure if not.

Args
farray1 a list of float values.
farray2 a list of float values.
err a float value.
msg Optional message to report on failure.

assertBetween

Asserts that value is between minv and maxv (inclusive).

assertCommandFails

Asserts a shell command fails and the error matches a regex in a list.

Args
command List or string representing the command to run.
regexes the list of regular expression strings.
env Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module.
close_fds Whether or not to close all open fd's in the child after forking.
msg Optional message to report on failure.

assertCommandSucceeds

Asserts that a shell command succeeds (i.e. exits with code 0).

Args
command List or string representing the command to run.
regexes List of regular expression byte strings that match success.
env Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module.
close_fds Whether or not to close all open fd's in the child after forking.
msg Optional message to report on failure.

assertContainsExactSubsequence

Asserts that "container" contains "subsequence" as an exact subsequence.

Asserts that "container" contains all the elements of "subsequence", in order, and without other elements interspersed. For example, [1, 2, 3] is an exact subsequence of [0, 0, 1, 2, 3, 0] but not of [0, 0, 1, 2, 0, 3, 0].

Args
container the list we're testing for subsequence inclusion.
subsequence the list we hope will be an exact subsequence of container.
msg Optional message to report on failure.

assertContainsInOrder

Asserts that the strings provided are found in the target in order.

This may be useful for checking HTML output.

Args
strings A list of strings, such as [ 'fox', 'dog' ]
target A target string in which to look for the strings, such as 'The quick brown fox jumped over the lazy dog'.
msg Optional message to report on failure.

assertContainsSubsequence

Asserts that "container" contains "subsequence" as a subsequence.

Asserts that "container" contains all the elements of "subsequence", in order, but possibly with other elements interspersed. For example, [1, 2, 3] is a subsequence of [0, 0, 1, 2, 0, 3, 0] but not of [0, 0, 1, 3, 0, 2, 0].

Args
container the list we're testing for subsequence inclusion.
subsequence the list we hope will be a subsequence of container.
msg Optional message to report on failure.

assertContainsSubset

Checks whether actual iterable is a superset of expected iterable.

assertCountEqual

An unordered sequence comparison asserting that the same elements, regardless of order. If the same element occurs more than once, it verifies that the elements occur the same number of times.

self.assertEqual(Counter(list(first)),
                 Counter(list(second)))

Example:

- [0, 1, 1] and [1, 0, 1] compare equal.
- [0, 0, 1] and [0, 1] compare unequal.

assertDTypeEqual

View source

Assert ndarray data type is equal to expected.

Args
target The numpy ndarray, or anything that can be converted into a numpy ndarray (including Tensor).
expected_dtype Expected data type.

assertDeviceEqual

View source

Asserts that the two given devices are the same.

Args
device1 A string device name or TensorFlow DeviceSpec object.
device2 A string device name or TensorFlow DeviceSpec object.
msg Optional message to report on failure.

assertDictContainsSubset

Checks whether dictionary is a superset of subset.

assertDictEqual

Raises AssertionError if a and b are not equal dictionaries.

Args
a A dict, the expected value.
b A dict, the actual value.
msg An optional str, the associated message.

Raises
AssertionError if the dictionaries are not equal.

assertEmpty

Asserts that an object has zero length.

Args
container Anything that implements the collections.abc.Sized interface.
msg Optional message to report on failure.

assertEndsWith

Asserts that actual.endswith(expected_end) is True.

Args
actual str
expected_end str
msg Optional message to report on failure.

assertEqual

Fail if the two objects are unequal as determined by the '==' operator.

assertEquals

assertFalse

Check that the expression is false.

assertGreater

Just like self.assertTrue(a > b), but with a nicer default message.

assertGreaterEqual

Just like self.assertTrue(a >= b), but with a nicer default message.

assertIn

Just like self.assertTrue(a in b), but with a nicer default message.

assertIs

Just like self.assertTrue(a is b), but with a nicer default message.

assertIsInstance

Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.

assertIsNone

Same as self.assertTrue(obj is None), with a nicer default message.

assertIsNot

Just like self.assertTrue(a is not b), but with a nicer default message.

assertIsNotNone

Included for symmetry with assertIsNone.

assertItemsEqual

An unordered sequence comparison asserting that the same elements, regardless of order. If the same element occurs more than once, it verifies that the elements occur the same number of times.

self.assertEqual(Counter(list(first)),
                 Counter(list(second)))

Example:

- [0, 1, 1] and [1, 0, 1] compare equal.
- [0, 0, 1] and [0, 1] compare unequal.

assertJsonEqual

Asserts that the JSON objects defined in two strings are equal.

A summary of the differences will be included in the failure message using assertSameStructure.

Args
first A string containing JSON to decode and compare to second.
second A string containing JSON to decode and compare to first.
msg Additional text to include in the failure message.

assertLen

Asserts that an object has the expected length.

Args
container Anything that implements the collections.abc.Sized interface.
expected_len The expected length of the container.
msg Optional message to report on failure.

assertLess

Just like self.assertTrue(a < b), but with a nicer default message.

assertLessEqual

Just like self.assertTrue(a <= b), but with a nicer default message.

assertListEqual

A list-specific equality assertion.

Args
list1 The first list to compare.
list2 The second list to compare.
msg Optional message to use on failure instead of a list of differences.

assertLogs

Fail unless a log message of level level or higher is emitted on logger_name or its children. If omitted, level defaults to INFO and logger defaults to the root logger.

This method must be used as a context manager, and will yield a recording object with two attributes: output and records. At the end of the context manager, the output attribute will be a list of the matching formatted log messages and the records attribute will be a list of the corresponding LogRecord objects.

Example::

with self.assertLogs('foo', level='INFO') as cm:
    logging.getLogger('foo').info('first message')
    logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
                             'ERROR:foo.bar:second message'])

assertMultiLineEqual

Asserts that two multi-line strings are equal.

assertNDArrayNear

View source

Asserts that two numpy arrays have near values.

Args
ndarray1 a numpy ndarray.
ndarray2 a numpy ndarray.
err a float. The maximum absolute difference allowed.
msg Optional message to report on failure.

assertNear

View source

Asserts that two floats are near each other.

Checks that |f1 - f2| < err and asserts a test failure if not.

Args
f1 A float value.
f2 A float value.
err A float value.
msg An optional string message to append to the failure message.

assertNoCommonElements

Checks whether actual iterable and expected iterable are disjoint.

assertNotAllClose

View source

Assert that two numpy arrays, or Tensors, do not have near values.

Args
a the first value to compare.
b the second value to compare.
**kwargs additional keyword arguments to be passed to the underlying assertAllClose call.

Raises
AssertionError If a and b are unexpectedly close at all elements.

assertNotAllEqual

View source

Asserts that two numpy arrays or Tensors do not have the same values.

Args
a the expected numpy ndarray or anything can be converted to one.
b the actual numpy ndarray or anything can be converted to one.
msg Optional message to report on failure.

assertNotAlmostEqual

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

Objects that are equal automatically fail.

assertNotAlmostEquals

assertNotEmpty

Asserts that an object has non-zero length.

Args
container Anything that implements the collections.abc.Sized interface.
msg Optional message to report on failure.

assertNotEndsWith

Asserts that actual.endswith(unexpected_end) is False.

Args
actual str
unexpected_end str
msg Optional message to report on failure.

assertNotEqual

Fail if the two objects are equal as determined by the '!=' operator.

assertNotEquals

assertNotIn

Just like self.assertTrue(a not in b), but with a nicer default message.

assertNotIsInstance

Included for symmetry with assertIsInstance.

assertNotRegex

Fail the test if the text matches the regular expression.

assertNotRegexpMatches

assertNotStartsWith

Asserts that actual.startswith(unexpected_start) is False.

Args
actual str
unexpected_start str
msg Optional message to report on failure.

assertProtoEquals

View source

Asserts that message is same as parsed expected_message_ascii.

Creates another prototype of message, reads the ascii message into it and then compares them using self._AssertProtoEqual().

Args
expected_message_maybe_ascii proto message in original or ascii form.
message the message to validate.
msg Optional message to report on failure.

assertProtoEqualsVersion

View source

assertRaises

Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments. If a different type of exception is raised, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

If called with the callable and arguments omitted, will return a context object used like this::

 with self.assertRaises(SomeException):
     do_something()

An optional keyword argument 'msg' can be provided when assertRaises is used as a context object.

The context manager keeps a reference to the exception as the 'exception' attribute. This allows you to inspect the exception after the assertion::

with self.assertRaises(SomeException) as cm:
    do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)

assertRaisesOpError

View source

assertRaisesRegex

Asserts that the message in a raised exception matches a regex.

Args
expected_exception Exception class expected to be raised.
expected_regex Regex (re.Pattern object or string) expected to be found in error message.
args Function to be called and extra positional args.
kwargs Extra kwargs.
msg Optional message used in case of failure. Can only be used when assertRaisesRegex is used as a context manager.

assertRaisesRegexp

Asserts that the message in a raised exception matches a regex.

Args
expected_exception Exception class expected to be raised.
expected_regex Regex (re.Pattern object or string) expected to be found in error message.
args Function to be called and extra positional args.
kwargs Extra kwargs.
msg Optional message used in case of failure. Can only be used when assertRaisesRegex is used as a context manager.

assertRaisesWithLiteralMatch

Asserts that the message in a raised exception equals the given string.

Unlike assertRaisesRegex, this method takes a literal string, not a regular expression.

with self.assertRaisesWithLiteralMatch(ExType, 'message'): DoSomething()

Args
expected_exception Exception class expected to be raised.
expected_exception_message String message expected in the raised exception. For a raise exception e, expected_exception_message must equal str(e).
callable_obj Function to be called, or None to return a context.
*args Extra args.
**kwargs Extra kwargs.

Returns
A context manager if callable_obj is None. Otherwise, None.

Raises
self.failureException if callable_obj does not raise a matching exception.

assertRaisesWithPredicateMatch

View source

Returns a context manager to enclose code expected to raise an exception.

If the exception is an OpError, the op stack is also included in the message predicate search.

Args
exception_type The expected type of exception that should be raised.
expected_err_re_or_predicate If this is callable, it should be a function of one argument that inspects the passed-in exception and returns True (success) or False (please fail the test). Otherwise, the error message is expected to match this regular expression partially.

Returns
A context manager to surround code that is expe