tf.test.TestCase

TensorFlow 2 version View source on GitHub

Class TestCase

Base class for tests that need to test TensorFlow.

Aliases:

__init__

View source

__init__(methodName='runTest')

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Child Classes

class failureException

Methods

__call__

__call__(
    *args,
    **kwds
)

Call self as a function.

__eq__

__eq__(other)

Return self==value.

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.abc.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<