Missed TensorFlow World? Check out the recap. Learn more

tf.test.TestCase

TensorFlow 2.0 version View source on GitHub

Class TestCase

Base class for tests that need to test TensorFlow.

Aliases:

  • Class tf.compat.v1.test.TestCase
  • Class tf.compat.v2.test.TestCase

__init__

View source

__init__(methodName='runTest')

Child Classes

class failureException

Methods

__call__

__call__(
    *args,
    **kwds
)

__eq__

__eq__(other)

addCleanup

addCleanup(
    function,
    *args,
    **kwargs
)

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

addTypeEqualityFunc(
    typeobj,
    function
)

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

assertAllClose(
    a,
    b,
    rtol=1e-06,
    atol=1e-06,
    msg=None
)

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

assertAllCloseAccordingToType(
    a,
    b,
    rtol=1e-06,
    atol=1e-06,
    float_rtol=1e-06,
    float_atol=1e-06,
    half_rtol=0.001,
    half_atol=0.001,
    bfloat16_rtol=0.01,
    bfloat16_atol=0.01,
    msg=None
)

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

assertAllEqual(
    a,
    b,
    msg=None
)

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

assertAllGreater(
    a,
    comparison_target
)

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

assertAllGreaterEqual(
    a,
    comparison_target
)

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

assertAllInRange(
    target,
    lower_bound,
    upper_bound,
    open_lower_bound=False,
    open_upper_bound=False
)

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

assertAllInSet(
    target,
    expected_set
)

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

assertAllLess(
    a,
    comparison_target
)

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

assertAllLessEqual(
    a,
    comparison_target
)

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

assertAlmostEqual(
    first,
    second,
    places=None,
    msg=None,
    delta=None
)

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

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

assertAlmostEquals

assertAlmostEquals(
    *args,
    **kwargs
)

assertArrayNear

View source

assertArrayNear(
    farray1,
    farray2,
    err,
    msg=None
)

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

assertBetween(
    value,
    minv,
    maxv,
    msg=None
)

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

assertCommandFails

assertCommandFails(
    command,
    regexes,
    env=None,
    close_fds=True,
    msg=None
)

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

assertCommandSucceeds(
    command,
    regexes=(b'',),
    env=None,
    close_fds=True,
    msg=None
)

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

assertContainsExactSubsequence(
    container,
    subsequence,
    msg=None
)

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

assertContainsInOrder(
    strings,
    target,
    msg=None
)

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

assertContainsSubsequence(
    container,
    subsequence,
    msg=None
)

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

assertContainsSubset(
    expected_subset,
    actual_set,
    msg=None
)

Checks whether actual iterable is a superset of expected iterable.

assertCountEqual

assertCountEqual(
    first,
    second,
    msg=None
)

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

assertDTypeEqual(
    target,
    expected_dtype
)

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

assertDeviceEqual(
    device1,
    device2,
    msg=None
)

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

assertDictContainsSubset(
    subset,
    dictionary,
    msg=None
)

Checks whether dictionary is a superset of subset.

assertDictEqual

assertDictEqual(
    a,
    b,
    msg=None
)

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

assertEmpty(
    container,
    msg=None
)

Asserts that an object has zero length.

Args:

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

assertEndsWith

assertEndsWith(
    actual,
    expected_end,
    msg=None
)

Asserts that actual.endswith(expected_end) is True.

Args:

  • actual: str
  • expected_end: str
  • msg: Optional message to report on failure.

assertEqual

assertEqual(
    first,
    second,
    msg=None
)

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

assertEquals

assertEquals(
    *args,
    **kwargs
)

assertFalse

assertFalse(
    expr,
    msg=None
)

Check that the expression is false.

assertGreater

assertGreater(
    a,
    b,
    msg=None
)

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

assertGreaterEqual

assertGreaterEqual(
    a,
    b,
    msg=None
)

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

assertIn

assertIn(
    member,
    container,
    msg=None
)

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

assertIs

assertIs(
    expr1,
    expr2,
    msg=None
)

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

assertIsInstance

assertIsInstance(
    obj,
    cls,
    msg=None
)

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

assertIsNone

assertIsNone(
    obj,
    msg=None
)

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

assertIsNot

assertIsNot(
    expr1,
    expr2,
    msg=None
)

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

assertIsNotNone

assertIsNotNone(
    obj,
    msg=None
)

Included for symmetry with assertIsNone.

assertItemsEqual

assertItemsEqual(
    first,
    second,
    msg=None
)

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

assertJsonEqual(
    first,
    second,
    msg=None
)

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 contining JSON to decode and compare to second.
  • second: A string contining JSON to decode and compare to first.
  • msg: Additional text to include in the failure message.

assertLen

assertLen(
    container,
    expected_len,
    msg=None
)

Asserts that an object has the expected length.

Args:

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

assertLess

assertLess(
    a,
    b,
    msg=None
)

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

assertLessEqual

assertLessEqual(
    a,
    b,
    msg=None
)

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

assertListEqual

assertListEqual(
    list1,
    list2,
    msg=None
)

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

assertLogs(
    logger=None,
    level=None
)

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

assertMultiLineEqual(
    first,
    second,
    msg=None,
    **kwargs
)

Asserts that two multi-line strings are equal.

assertNDArrayNear

View source

assertNDArrayNear(
    ndarray1,
    ndarray2,
    err,
    msg=None
)

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

assertNear(
    f1,
    f2,
    err,
    msg=None
)

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

assertNoCommonElements(
    expected_seq,
    actual_seq,
    msg=None
)

Checks whether actual iterable and expected iterable are disjoint.

assertNotAllClose

View source

assertNotAllClose(
    a,
    b,
    **kwargs
)

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.

assertNotAlmostEqual

assertNotAlmostEqual(
    first,
    second,
    places=None,
    msg=None,
    delta=None
)

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

Objects that are equal automatically fail.

assertNotAlmostEquals