31

Is there any way to run Tensorflow unit tests manually? I want to perform sanity checks while modifying TF source code.

I see there are many _test.py files with classes that perform many test operations and I can't figure out how to run them. There should be an easy way?

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
Maksym Diachenko
  • 552
  • 1
  • 4
  • 11

2 Answers2

41

The easiest way to run the TensorFlow unit tests is using Bazel, assuming you have downloaded the source from Git:

# All tests (for C++ changes).
$ bazel test //tensorflow/...

# All Python tests (for Python front-end changes).
$ bazel test //tensorflow/python/...

# All tests (with GPU support).
$ bazel test -c opt --config=cuda //tensorflow/...
$ bazel test -c opt --config=cuda //tensorflow/python/...
mrry
  • 125,488
  • 26
  • 399
  • 400
  • And how would you run these tests with gpu support? Just directly executing them only seems to use CPU: `I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 16 I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 16 ` – panmari Dec 21 '15 at 16:13
  • I updated the answer to show how to build and run with GPU support. (Note that those log messages will be produced in CPU *and* GPU modes.) – mrry Dec 21 '15 at 16:16
  • Sure, I just wanted to emphasize that only the CPU is listed right now. Thanks for your update! – panmari Dec 21 '15 at 16:18
  • Thank you ! How to run a single one ? For example , array_ops_test.cc – DunkOnly Oct 09 '16 at 04:16
  • 10
    `bazel test //tensorflow/core:ops_array_ops_test` – DunkOnly Oct 09 '16 at 06:08
  • How to run it under gdb? – DunkOnly Oct 17 '16 at 03:41
  • Do we need `--cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"` for `bazel test` options? – skytree May 31 '19 at 21:09
8

In addition to the above answer, you can run individual tests as shown instead of full packages, which can save a significant amount of time.

bazel run //tensorflow/python/kernel_tests:string_split_op_test

bazel run //tensorflow/python:special_math_ops_test

Or you can go to the individual directory and run all the tests there

cd python/kernel_tests
bazel run :one_hot_op_test
Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33