<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/datasets/blob/v4.9.10/tensorflow_datasets/testing/test_utils.py#L364-L436) |

Execute the decorated test in both graph mode and eager mode.

    tfds.testing.run_in_graph_and_eager_modes(
        func=None, config=None, use_gpu=True
    )

This function returns a decorator intended to be applied to test methods in
a `test_case.TestCase` class. Doing so will cause the contents of the test
method to be executed twice - once in graph mode, and once with eager
execution enabled. This allows unittests to confirm the equivalence between
eager and graph execution.

> [!NOTE]
> **Note:** This decorator can only be used when executing eagerly in the outer scope.

For example, consider the following unittest:

    class SomeTest(tfds.testing.TestCase):

      @tfds.testing.run_in_graph_and_eager_modes
      def test_foo(self):
        x = tf.constant([1, 2])
        y = tf.constant([3, 4])
        z = tf.add(x, y)
        self.assertAllEqual([4, 6], self.evaluate(z))

    if __name__ == '__main__':
      tfds.testing.test_main()

This test validates that [`tf.add()`](https://www.tensorflow.org/api_docs/python/tf/math/add) has the same behavior when computed with
eager execution enabled as it does when constructing a TensorFlow graph and
executing the `z` tensor with a session.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `func` | function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. |
| `config` | An optional config_pb2.ConfigProto to use to configure the session when executing graphs. |
| `use_gpu` | If True, attempt to run as many operations as possible on GPU. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Returns a decorator that will run the decorated test method twice: once by constructing and executing a graph in a session and once with eager execution enabled. ||

<br />