Join us at TensorFlow World, Oct 28-31. Use code TF20 for 20% off select passes.

# tfds.testing.DatasetBuilderTestCase

View source

## Class DatasetBuilderTestCase

Inherit this class to test your DatasetBuilder class.

Inherits From: SubTestCase

You must set the following class attributes:

• DATASET_CLASS: class object of DatasetBuilder you want to test.

You may set the following class attributes:

• VERSION: str. The version used to run the test. eg: '1.2.*'. Defaults to None (canonical version).
• BUILDER_CONFIG_NAMES_TO_TEST: list[str], the list of builder configs that should be tested. If None, all the BUILDER_CONFIGS from the class will be tested.
• DL_EXTRACT_RESULT: dict[str], the returned result of mocked download_and_extract method. The values should be the path of files present in the fake_examples directory, relative to that directory. If not specified, path to fake_examples will always be returned.
• EXAMPLE_DIR: str, the base directory in in which fake examples are contained. Optional; defaults to tensorflow_datasets/testing/test_data/fake_examples/.
• OVERLAPPING_SPLITS: list[str], splits containing examples from other splits (e.g. a "example" split containing pictures from other splits).
• MOCK_OUT_FORBIDDEN_OS_FUNCTIONS: bool, defaults to True. Set to False to disable checks preventing usage of os or builtin functions instead of recommended tf.io.gfile API.

This test case will check for the following:

• the dataset builder is correctly registered, i.e. tfds.load(name) works;
• the dataset builder can read the fake examples stored in testing/test_data/fake_examples/${dataset_name}; • the dataset builder can produce serialized data; • the dataset builder produces a valid Dataset object from serialized data • in eager mode; • in graph mode. • the produced Dataset examples have the expected dimensions and types; • the produced Dataset has and the expected number of examples; • a example is not part of two splits, or one of these splits is whitelisted in OVERLAPPING_SPLITS. ## __init__ __init__(methodName='runTest')  ## Child Classes class failureException ## Methods ### __call__ __call__( *args, **kwds )  ### __eq__ __eq__(other)  ### __ne__ __ne__(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). addExternalLink( name, url, contact_email='', component_id='', description='', foreground_color='#000000', icon_name='' )  Add an external link to the testcase. #### Args: • name: str- name of the external link • url: str- url to link • contact_email: str- contact email for this link • component_id: str- buganizer component id • description: str- a detailed description for the link • foreground_color: str- foreground color for the link • icon_name: str- icon name for the link ### 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 assertAllClose( *args, **kwds )  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 assertAllCloseAccordingToType( *args, **kwds )  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( d1, d2 )  Same as assertAllEqual but with RaggedTensor support. ### assertAllEqualNested View source assertAllEqualNested( d1, d2 )  Same as assertAllEqual but compatible with nested dict. ### assertAllGreater assertAllGreater( *args, **kwds )  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 assertAllGreaterEqual( *args, **kwds )  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 assertAllInRange( *args, **kwds )  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 assertAllInSet( *args, **kwds )  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 assertAllLess( *args, **kwds )  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 assertAllLessEqual( *args, **kwds )  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 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 assertAlmostEquals( 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 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. ### assertArrayNear assertArrayNear( *args, **kwds )  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=('',), 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( expected_seq, actual_seq, msg=None )  Tests two sequences have the same elements regardless of order. It tests that the first sequence contains the same elements as the second, regardless of their order. When they don't, an error message listing the differences between the sequences will be generated. Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: self.assertEqual(Counter(list(expected_seq)), Counter(list(actual_seq)))  but works with sequences of unhashable objects as well. #### Example: • [0, 1, 1] and [1, 0, 1] compare equal. • [0, 0, 1] and [0, 1] compare unequal. #### Args: • expected_seq: A sequence containing elements we are expecting. • actual_seq: The sequence that we are testing. • msg: The message to be printed if the test fails. ### assertDTypeEqual assertDTypeEqual( *args, **kwds )  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 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( expected, actual, msg=None )  Checks whether actual is a superset of expected. ### 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 assertEquals( first, second, msg=None )  Fail if the two objects are unequal as determined by the '==' operator. ### 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( expected_seq, actual_seq, msg=None )  Deprecated, please use assertCountEqual instead. This is equivalent to assertCountEqual in Python 3. An implementation of assertCountEqual is also provided by absltest.TestCase for Python 2. #### Args: • expected_seq: A sequence containing elements we are expecting. • actual_seq: The sequence that we are testing. • msg: The message to be printed if the test fails. ### 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 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 assertLen( container, expected_len, msg=None )  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 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( *args, **kwds )  ### assertMultiLineEqual assertMultiLineEqual( first, second, msg=None, **kwargs )  Asserts that two multi-line strings are equal. ### assertNDArrayNear assertNDArrayNear( *args, **kwds )  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 assertNear( *args, **kwds )  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. ### assertNestedListAlmostEqual View source assertNestedListAlmostEqual( a, b, places=7, context='value' )  ### assertNoCommonElements assertNoCommonElements( expected_seq, actual_seq, msg=None )  Checks whether actual iterable and expected iterable are disjoint. ### assertNotAllClose assertNotAllClose( *args, **kwds )  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 assertNotAllEqual( *args, **kwds )  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 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 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 assertNotAlmostEquals( 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 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. ### assertNotEmpty assertNotEmpty( container, msg=None )  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 assertNotEndsWith( actual, unexpected_end, msg=None )  Asserts that actual.endswith(unexpected_end) is False. #### Args: • actual: str • unexpected_end: str • msg: Optional message to report on failure. ### assertNotEqual assertNotEqual( first, second, msg=None )  Fail if the two objects are equal as determined by the '!=' operator. ### assertNotEquals assertNotEquals( first, second, msg=None )  Fail if the two objects are equal as determined by the '!=' operator. ### assertNotIn assertNotIn( member, container, msg=None )  Just like self.assertTrue(a not in b), but with a nicer default message. ### assertNotIsInstance assertNotIsInstance( obj, cls, msg=None )  Included for symmetry with assertIsInstance. ### assertNotRegex assertNotRegex( *args, **kwargs )  ### assertNotRegexpMatches assertNotRegexpMatches( text, unexpected_regexp, msg=None )  Fail the test if the text matches the regular expression. ### assertNotStartsWith assertNotStartsWith( actual, unexpected_start, msg=None )  Asserts that actual.startswith(unexpected_start) is False. #### Args: • actual: str • unexpected_start: str • msg: Optional message to report on failure. ### assertProtoEquals assertProtoEquals( expected_message_maybe_ascii, message, msg=None )  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 assertProtoEqualsVersion( expected, actual, producer=versions.GRAPH_DEF_VERSION, min_consumer=versions.GRAPH_DEF_VERSION_MIN_CONSUMER, msg=None )  ### assertRaggedAlmostEqual View source assertRaggedAlmostEqual( a, b, places=7 )  ### assertRaggedEqual View source assertRaggedEqual( a, b )  Asserts that two potentially ragged tensors are equal. ### assertRaises assertRaises( excClass, callableObj=None, *args, **kwargs )  Fail unless an exception of class excClass is raised by callableObj when invoked with arguments args and keyword arguments kwargs. 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 callableObj omitted or None, will return a context object used like this::  with self.assertRaises(SomeException): do_something()  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 assertRaisesOpError(expected_err_re_or_predicate)  ### assertRaisesRegex assertRaisesRegex( *args, **kwargs )  ### assertRaisesRegexp assertRaisesRegexp( expected_exception, expected_regexp, callable_obj=None, *args, **kwargs )  Asserts that the message in a raised exception matches a regexp. #### Args: • expected_exception: Exception class expected to be raised. • expected_regexp: Regexp (re pattern object or string) expected to be found in error message. • callable_obj: Function to be called. • args: Extra args. • kwargs: Extra kwargs. ### assertRaisesWithLiteralMatch assertRaisesWithLiteralMatch( expected_exception, expected_exception_message, callable_obj=None, *args, **kwargs )  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 assertRaisesWithPredicateMatch( err_type, predicate )  ### assertRaisesWithRegexpMatch assertRaisesWithRegexpMatch( expected_exception, expected_regexp, callable_obj=None, *args, **kwargs )  Asserts that the message in a raised exception matches a regexp. #### Args: • expected_exception: Exception class expected to be raised. • expected_regexp: Regexp (re pattern object or string) expected to be found in error message. • callable_obj: Function to be called. • args: Extra args. • kwargs: Extra kwargs. ### assertRegex assertRegex( *args, **kwargs )  ### assertRegexMatch assertRegexMatch( actual_str, regexes, message=None )  Asserts that at least one regex in regexes matches str. If possible you should use assertRegex, which is a simpler version of this method. assertRegex takes a single regular expression (a string or re compiled object) instead of a list. #### Notes: 1. This function uses substring matching, i.e. the matching succeeds if any substring of the error message matches any regex in the list. This is more convenient for the user than full-string matching. 2. If regexes is the empty list, the matching will always fail. 3. Use regexes=[''] for a regex that will always pass. 4. '.' matches any single character except the newline. To match any character, use '(.|\n)'. 5. '^' matches the beginning of each line, not just the beginning of the string. Similarly, '$' matches the end of each line.

6. An exception will be thrown if regexes contains an invalid regex.

#### Args:

• actual_str: The string we try to match with the items in regexes.
• regexes: The regular expressions we want to match against str. See "Notes" above for detailed notes on how this is interpreted.
• message: The message to be printed if the test fails.

### assertRegexpMatches

assertRegexpMatches(
text,
expected_regexp,
msg=None
)


Fail the test unless the text matches the regular expression.

### assertSameElements

assertSameElements(
expected_seq,
actual_seq,
msg=None
)


Asserts that two sequences have the same elements (in any order).

This method, unlike assertCountEqual, doesn't care about any duplicates in the expected and actual sequences.

assertSameElements([1, 1, 1, 0, 0, 0], [0, 1]) # Doesn't raise an AssertionError

If possible, you should use assertCountEqual instead of assertSameElements.

#### Args:

• expected_seq: A sequence containing elements we are expecting.
• actual_seq: The sequence that we are testing.
• msg: The message to be printed if the test fails.

### assertSameStructure

assertSameStructure(
a,
b,
aname='a',
bname='b',
msg=None
)


Asserts that two values contain the same structural content.

The two arguments should be data trees consisting of trees of dicts and lists. They will be deeply compared by walking into the contents of dicts and lists; other items will be compared using the == operator. If the two structures differ in content, the failure message will indicate the location within the structures where the first difference is found. This may be helpful when comparing large structures.

Mixed Sequence and Set types are supported. Mixed Mapping types are supported, but the order of the keys will not be considered in the comparison.

#### Args:

• a: The first structure to compare.
• b: The second structure to compare.
• aname: Variable name to use for the first structure in assertion messages.
• bname: Variable name to use for the second structure.
• msg: Additional text to include in the failure message.

### assertSequenceAlmostEqual

assertSequenceAlmostEqual(
expected_seq,
actual_seq,
places=None,
msg=None,
delta=None
)


An approximate equality assertion for ordered sequences.

Fail if the two sequences are unequal as determined by their value differences rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between each value in the two sequences 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 sequences compare equal then they will automatically compare almost equal.

#### Args:

• expected_seq: A sequence containing elements we are expecting.
• actual_seq: The sequence that we are testing.
• places: The number of decimal places to compare.
• msg: The message to be printed if the test fails.
• delta: The OK difference between compared values.

### assertSequenceEqual

assertSequenceEqual(
seq1,
seq2,
msg=None,
seq_type=None
)


An equality assertion for ordered sequences (like lists and tuples).

For the purposes of this function, a valid ordered sequence type is one which can be indexed, has a length, and has an equality operator.

#### Args:

• seq1: The first sequence to compare.
• seq2: The second sequence to compare.
• seq_type: The expected datatype of the sequences, or None if no datatype should be enforced.
• msg: Optional message to use on failure instead of a list of differences.

### assertSequenceStartsWith

assertSequenceStartsWith(
prefix,
whole,
msg=None
)


An equality assertion for the beginning of ordered sequences.

If prefix is an empty sequence, it will raise an error unless whole is also an empty sequence.

If prefix is not a sequence, it will raise an error if the first element of whole does not match.

#### Args:

• prefix: A sequence expected at the beginning of the whole parameter.
• whole: The sequence in which to look for prefix.
• msg: Optional message to report on failure.

### assertSetEqual

assertSetEqual(
set1,
set2,
msg=None
)


A set-specific equality assertion.

#### Args:

• set1: The first set to compare.
• set2: The second set to compare.
• msg: Optional message to use on failure instead of a list of differences.

assertSetEqual uses ducktyping to support different types of sets, and is optimized for sets specifically (parameters must support a difference method).

### assertShapeEqual

assertShapeEqual(
np_array,
tf_tensor,
msg=None
)


Asserts that a Numpy ndarray and a TensorFlow tensor have the same shape.

#### Args:

• np_array: A Numpy ndarray or Numpy scalar.
• tf_tensor: A Tensor.
• msg: Optional message to report on failure.

#### Raises:

• TypeError: If the arguments have the wrong type.

### assertStartsWith

assertStartsWith(
actual,
expected_start,
msg=None
)


Assert that actual.startswith(expected_start) is True.

#### Args:

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

### assertTotallyOrdered

assertTotallyOrdered(
*groups,
**kwargs
)


Asserts that total ordering has been implemented correctly.

For example, say you have a class A that compares only on its attribute x. Comparators other than lt are omitted for brevity.

class A(object): def init(self, x, y): self.x = x self.y = y

def hash(self): return hash(self.x)

def lt(self, other): try: return self.x < other.x except AttributeError: return NotImplemented

assertTotallyOrdered will check that instances can be ordered correctly. For example,

self.assertTotallyOrdered( [None], # None should come before everything else. [1], # Integers sort earlier. [A(1, 'a')], [A(2, 'b')], # 2 is after 1. [A(3, 'c'), A(3, 'd')], # The second argument is irrelevant. [A(4, 'z')], ['foo']) # Strings sort last.

#### Args:

• *groups: A list of groups of elements. Each group of elements is a list of objects that are equal. The elements in each group must be less than the elements in the group after it. For example, these groups are totally ordered: [None], [1], [2, 2], [3]. **kwargs: optional msg keyword argument can be passed.

### assertTrue

assertTrue(
expr,
msg=None
)


Check that the expression is true.

### assertTupleEqual

assertTupleEqual(
tuple1,
tuple2,
msg=None
)


A tuple-specific equality assertion.

#### Args:

• tuple1: The first tuple to compare.
• tuple2: The second tuple to compare.
• msg: Optional message to use on failure instead of a list of differences.

### assertUrlEqual

assertUrlEqual(
a,
b,
msg=None
)


Asserts that urls are equal, ignoring ordering of query params.

### assert_

assert_(
expr,
msg=None
)


Check that the expression is true.

### cached_session

cached_session(
*args,
**kwds
)


Returns a TensorFlow Session for use in executing tests.

This method behaves differently than self.session(): for performance reasons cached_session will by default reuse the same session within the same test. The session returned by this function will only be closed at the end of the test (in the TearDown function).

Use the use_gpu and force_gpu options to control where ops are run. If force_gpu is True, all ops are pinned to /device:GPU:0. Otherwise, if use_gpu is True, TensorFlow tries to run as many ops on the GPU as possible. If both force_gpu anduse_gpu are False, all ops are pinned to the CPU.

#### Example:

class MyOperatorTest(test_util.TensorFlowTestCase):
def testMyOperator(self):
with self.cached_session(use_gpu=True) as sess:
valid_input = [1.0, 2.0, 3.0, 4.0, 5.0]
result = MyOperator(valid_input).eval()
self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0]
invalid_input = [-1.0, 2.0, 7.0]
with self.assertRaisesOpError("negative input not supported"):
MyOperator(invalid_input).eval()


#### Args:

• graph: Optional graph to use during the returned session.
• config: An optional config_pb2.ConfigProto to use to configure the session.
• use_gpu: If True, attempt to run as many ops as possible on GPU.
• force_gpu: If True, pin all ops to /device:GPU:0.

#### Yields:

A Session object that should be used as a context manager to surround the graph building and execution code in a test case.

### captureWritesToStream

captureWritesToStream(
*args,
**kwds
)


A context manager that captures the writes to a given stream.

This context manager captures all writes to a given stream inside of a CapturedWrites object. When this context manager is created, it yields the CapturedWrites object. The captured contents can be accessed by calling .contents() on the CapturedWrites.

For this function to work, the stream must have a file descriptor that can be modified using os.dup and os.dup2, and the stream must support a .flush() method. The default python sys.stdout and sys.stderr are examples of this. Note that this does not work in Colab or Jupyter notebooks, because those use alternate stdout streams.

#### Example:

class MyOperatorTest(test_util.TensorFlowTestCase):
def testMyOperator(self):
input = [1.0, 2.0, 3.0, 4.0, 5.0]
with self.captureWritesToStream(sys.stdout) as captured:
result = MyOperator(input).eval()
self.assertStartsWith(captured.contents(), "This was printed.")


#### Args:

• stream: The stream whose writes should be captured. This stream must have a file descriptor, support writing via using that file descriptor, and must have a .flush() method.

#### Yields:

A CapturedWrites object that contains all writes to the specified stream made during this context.

### checkedThread

checkedThread(
target,
args=None,
kwargs=None
)


Returns a Thread wrapper that asserts 'target' completes successfully.

This method should be used to create all threads in test cases, as otherwise there is a risk that a thread will silently fail, and/or assertions made in the thread will not be respected.

#### Args:

• target: A callable object to be executed in the thread.
• args: The argument tuple for the target invocation. Defaults to ().
• kwargs: A dictionary of keyword arguments for the target invocation. Defaults to {}.

### countTestCases

countTestCases()


### create_tempdir

create_tempdir(
name=None,
cleanup=None
)


Create a temporary directory specific to the test.

NOTE: The directory and its contents will be recursively cleared before creation. This ensures that there is no pre-existing state.

This creates a named directory on disk that is isolated to this test, and will be properly cleaned up by the test. This avoids several pitfalls of creating temporary directories for test purposes, as well as makes it easier to setup directories and verify their contents.

See also: create_tempfile() for creating temporary files.

#### Args:

• name: Optional name of the directory. If not given, a unique name will be generated and used.
• cleanup: Optional cleanup policy on when/if to remove the directory (and all its contents) at the end of the test. If None, then uses self.tempfile_cleanup.

#### Returns:

A _TempDir representing the created directory.

### create_tempfile

create_tempfile(
file_path=None,
content=None,
mode='w',
encoding='utf8',
errors='strict',
cleanup=None
)


Create a temporary file specific to the test.

This creates a named file on disk that is isolated to this test, and will be properly cleaned up by the test. This avoids several pitfalls of creating temporary files for test purposes, as well as makes it easier to setup files, their data, read them back, and inspect them when a test fails.

NOTE: This will zero-out the file. This ensures there is no pre-existing state.

See also: create_tempdir() for creating temporary directories.

#### Args:

• file_path: Optional file path for the temp file. If not given, a unique file name will be generated and used. Slashes are allowed in the name; any missing intermediate directories will be created. NOTE: This path is the path that will be cleaned up, including any directories in the path, e.g., 'foo/bar/baz.txt' will rm -r foo.
• content: Optional string or bytes to initially write to the file. If not specified, then an empty file is created.
• mode: Mode string to use when writing content. Only used if content is non-empty.
• encoding: Encoding to use when writing string content. Only used if content is text.
• errors: How to handle text to bytes encoding errors. Only used if content is text.
• cleanup: Optional cleanup policy on when/if to remove the directory (and all its contents) at the end of the test. If None, then uses self.tempfile_cleanup.

#### Returns:

A _TempFile representing the created file.

### debug

debug()


Run the test without collecting errors in a TestResult

### defaultTestResult

defaultTestResult()


### doCleanups

doCleanups()


Execute all cleanup functions. Normally called for you after tearDown.

### eval_to_list

View source

eval_to_list(tensor)


### evaluate

evaluate(tensors)


Evaluates tensors and returns numpy values.

#### Args:

• tensors: A Tensor or a nested list/tuple of Tensors.

#### Returns:

tensors numpy values.

### fail

fail(
msg=None,
prefix=None
)


Fail immediately with the given message, optionally prefixed.

### failIf

failIf(
*args,
**kwargs
)


### failIfAlmostEqual

failIfAlmostEqual(
*args,
**kwargs
)


### failIfEqual

failIfEqual(
*args,
**kwargs
)


### failUnless

failUnless(
*args,
**kwargs
)


### failUnlessAlmostEqual

failUnlessAlmostEqual(
*args,
**kwargs
)


### failUnlessEqual

failUnlessEqual(
*args,
**kwargs
)


### failUnlessRaises

failUnlessRaises(
*args,
**kwargs
)


### gcs_access

gcs_access(
*args,
**kwds
)

getExternalLinks()


Return any externallinks that the user has provided.

#### Returns:

dict of names to _ExternalLink namedtuples.

### getRecordedProperties

getRecordedProperties()


Return any properties that the user has recorded.

### get_temp_dir

get_temp_dir()


Returns a unique temporary directory for the test to use.

If you call this method multiple times during in a test, it will return the same folder. However, across different runs the directories will be different. This will ensure that across different runs tests will not be able to pollute each others environment. If you need multiple unique directories within a single test, you should use tempfile.mkdtemp as follows: tempfile.mkdtemp(dir=self.get_temp_dir()):

#### Returns:

string, the path to the unique temporary directory created for this test.

### id

id()


Returns the descriptive ID of the test.

This is used internally by the unittesting framework to get a name for the test to be used in reports.

The test id.

### recordProperty

recordProperty(
property_name,
property_value
)


Record an arbitrary property for later use.

#### Args:

• property_name: str, name of property to record; must be a valid XML attribute name
• property_value: value of property; must be valid XML attribute value

### run

run(result=None)


### session

session(
*args,
**kwds
)


Returns a TensorFlow Session for use in executing tests.

Note that this will set this session and the graph as global defaults.

Use the use_gpu and force_gpu options to control where ops are run. If force_gpu is True, all ops are pinned to /device:GPU:0. Otherwise, if use_gpu is True, TensorFlow tries to run as many ops on the GPU as possible. If both force_gpu anduse_gpu are False, all ops are pinned to the CPU.

#### Example:

class MyOperatorTest(test_util.TensorFlowTestCase):
def testMyOperator(self):
with self.session(use_gpu=True):
valid_input = [1.0, 2.0, 3.0, 4.0, 5.0]
result = MyOperator(valid_input).eval()
self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0]
invalid_input = [-1.0, 2.0, 7.0]
with self.assertRaisesOpError("negative input not supported"):
MyOperator(invalid_input).eval()


#### Args:

• graph: Optional graph to use during the returned session.
• config: An optional config_pb2.ConfigProto to use to configure the session.
• use_gpu: If True, attempt to run as many ops as possible on GPU.
• force_gpu: If True, pin all ops to /device:GPU:0.

#### Yields:

A Session object that should be used as a context manager to surround the graph building and execution code in a test case.

### setUp

View source

setUp()


### setUpClass

View source

@classmethod
setUpClass(cls)


### shortDescription

shortDescription()


Formats both the test method name and the first line of its docstring.

If no docstring is given, only returns the method name.

This method overrides unittest.TestCase.shortDescription(), which only returns the first line of the docstring, obscuring the name of the test upon failure.

#### Returns:

• desc: A short description of a test method.

### skipTest

skipTest(reason)


Skip this test.

### subTest

subTest(
*args,
**kwds
)


Return a context manager that will run the enclosed subtest.

### tearDown

View source

tearDown()


### tearDownClass

tearDownClass(cls)


Hook method for deconstructing the class fixture after running all tests in the class.

### test_baseclass

View source

test_baseclass()


### test_download_and_prepare_as_dataset

View source

test_download_and_prepare_as_dataset(
*args,
**kwargs
)


Run the decorated test method.

### test_info

View source

test_info()


### test_registered

View source

test_registered()


### test_session

test_session(
*args,
**kwds
)


## Class Members

• BUILDER_CONFIG_NAMES_TO_TEST = None
• DATASET_CLASS = None
• DL_EXTRACT_RESULT = None
• EXAMPLE_DIR = None
• MOCK_MONARCH = True
• MOCK_OUT_FORBIDDEN_OS_FUNCTIONS = True
• OVERLAPPING_SPLITS
• VERSION = None
• longMessage = True
• maxDiff = 1600
• tempfile_cleanup