diff options
author | Gunhan Gulsoy <gunan@google.com> | 2018-01-11 12:03:29 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-01-11 12:07:10 -0800 |
commit | f433d35dabd61d1c551a8c08def24982dbf920b9 (patch) | |
tree | 90605eabc7ac202e6f08e244d0d403d9df1ee292 /tensorflow/python/platform | |
parent | b3ecb480c344d8bab23d085fdd3547ed99210eb6 (diff) |
Enable stacktrace printing ont test failures for python tests.
PiperOrigin-RevId: 181642475
Diffstat (limited to 'tensorflow/python/platform')
-rw-r--r-- | tensorflow/python/platform/stacktrace_handler.i | 27 | ||||
-rw-r--r-- | tensorflow/python/platform/stacktrace_handler_test.py | 80 | ||||
-rw-r--r-- | tensorflow/python/platform/test.py | 1 |
3 files changed, 108 insertions, 0 deletions
diff --git a/tensorflow/python/platform/stacktrace_handler.i b/tensorflow/python/platform/stacktrace_handler.i new file mode 100644 index 0000000000..be4eea4c2f --- /dev/null +++ b/tensorflow/python/platform/stacktrace_handler.i @@ -0,0 +1,27 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +%include "tensorflow/python/platform/base.i" + +%{ +#include "tensorflow/core/platform/stacktrace_handler.h" +%} + +%ignoreall +%unignore tensorflow; +%unignore tensorflow::testing; +%unignore tensorflow::testing::InstallStacktraceHandler; +%include "tensorflow/core/platform/stacktrace_handler.h" +%unignoreall diff --git a/tensorflow/python/platform/stacktrace_handler_test.py b/tensorflow/python/platform/stacktrace_handler_test.py new file mode 100644 index 0000000000..3f0e534f4c --- /dev/null +++ b/tensorflow/python/platform/stacktrace_handler_test.py @@ -0,0 +1,80 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================= +"""Test to make sure stack trace is generated in case of test failures.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import argparse +import os +import signal +import subprocess +import sys + +from tensorflow.python.platform import test +from tensorflow.python.platform import tf_logging as logging + + +# FLAGS defined at the bottom: +# child (bool) set to true if we are running in the child process. +FLAGS = None + +_CHILD_FLAG_HELP = 'Boolean. Set to true if this is the child process.' + + +class StacktraceHandlerTest(test.TestCase): + + def testChildProcessKillsItself(self): + if FLAGS.child: + os.kill(os.getpid(), signal.SIGABRT) + + def testGeneratesStacktrace(self): + if FLAGS.child: + return + + # Subprocess sys.argv[0] with --child=True + if sys.executable: + child_process = subprocess.Popen( + [sys.executable, sys.argv[0], '--child=True'], cwd=os.getcwd(), + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + else: + child_process = subprocess.Popen( + [sys.argv[0], '--child=True'], cwd=os.getcwd(), + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + # Capture its output. capture both stdout and stderr and append them. + # We are not worried about timing or order of messages in this test. + child_output = child_process.stdout.read() + child_process.stderr.read() + + # Make sure the child process is dead before we proceed. + child_process.wait() + + logging.info('Output from the child process:') + logging.info(child_output) + + # Verify a stack trace is printed. + self.assertIn(b'PyEval_EvalFrame', child_output) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument( + '--child', type=bool, default=False, help=_CHILD_FLAG_HELP) + FLAGS, unparsed = parser.parse_known_args() + + # Now update argv, so that unittest library does not get confused. + sys.argv = [sys.argv[0]] + unparsed + test.main() diff --git a/tensorflow/python/platform/test.py b/tensorflow/python/platform/test.py index 72025f6717..ec280c6e1e 100644 --- a/tensorflow/python/platform/test.py +++ b/tensorflow/python/platform/test.py @@ -70,6 +70,7 @@ StubOutForTesting = _googletest.StubOutForTesting # pylint: disable=invalid-nam def main(argv=None): """Runs all unit tests.""" + _test_util.InstallStackTraceHandler() return _googletest.main(argv) |