gtest_break_on_failure_unittest.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2006, Google Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are
00008 # met:
00009 #
00010 #     * Redistributions of source code must retain the above copyright
00011 # notice, this list of conditions and the following disclaimer.
00012 #     * Redistributions in binary form must reproduce the above
00013 # copyright notice, this list of conditions and the following disclaimer
00014 # in the documentation and/or other materials provided with the
00015 # distribution.
00016 #     * Neither the name of Google Inc. nor the names of its
00017 # contributors may be used to endorse or promote products derived from
00018 # this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00023 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 
00032 """Unit test for Google Test's break-on-failure mode.
00033 
00034 A user can ask Google Test to seg-fault when an assertion fails, using
00035 either the GTEST_BREAK_ON_FAILURE environment variable or the
00036 --gtest_break_on_failure flag.  This script tests such functionality
00037 by invoking gtest_break_on_failure_unittest_ (a program written with
00038 Google Test) with different environments and command line flags.
00039 """
00040 
00041 __author__ = 'wan@google.com (Zhanyong Wan)'
00042 
00043 import gtest_test_utils
00044 import os
00045 import sys
00046 
00047 
00048 # Constants.
00049 
00050 IS_WINDOWS = os.name == 'nt'
00051 
00052 # The environment variable for enabling/disabling the break-on-failure mode.
00053 BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
00054 
00055 # The command line flag for enabling/disabling the break-on-failure mode.
00056 BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
00057 
00058 # The environment variable for enabling/disabling the throw-on-failure mode.
00059 THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
00060 
00061 # The environment variable for enabling/disabling the catch-exceptions mode.
00062 CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
00063 
00064 # Path to the gtest_break_on_failure_unittest_ program.
00065 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00066     'gtest_break_on_failure_unittest_')
00067 
00068 
00069 environ = gtest_test_utils.environ
00070 SetEnvVar = gtest_test_utils.SetEnvVar
00071 
00072 # Tests in this file run a Google-Test-based test program and expect it
00073 # to terminate prematurely.  Therefore they are incompatible with
00074 # the premature-exit-file protocol by design.  Unset the
00075 # premature-exit filepath to prevent Google Test from creating
00076 # the file.
00077 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
00078 
00079 
00080 def Run(command):
00081   """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
00082 
00083   p = gtest_test_utils.Subprocess(command, env=environ)
00084   if p.terminated_by_signal:
00085     return 1
00086   else:
00087     return 0
00088 
00089 
00090 # The tests.
00091 
00092 
00093 class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
00094   """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
00095   the --gtest_break_on_failure flag to turn assertion failures into
00096   segmentation faults.
00097   """
00098 
00099   def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
00100     """Runs gtest_break_on_failure_unittest_ and verifies that it does
00101     (or does not) have a seg-fault.
00102 
00103     Args:
00104       env_var_value:    value of the GTEST_BREAK_ON_FAILURE environment
00105                         variable; None if the variable should be unset.
00106       flag_value:       value of the --gtest_break_on_failure flag;
00107                         None if the flag should not be present.
00108       expect_seg_fault: 1 if the program is expected to generate a seg-fault;
00109                         0 otherwise.
00110     """
00111 
00112     SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
00113 
00114     if env_var_value is None:
00115       env_var_value_msg = ' is not set'
00116     else:
00117       env_var_value_msg = '=' + env_var_value
00118 
00119     if flag_value is None:
00120       flag = ''
00121     elif flag_value == '0':
00122       flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
00123     else:
00124       flag = '--%s' % BREAK_ON_FAILURE_FLAG
00125 
00126     command = [EXE_PATH]
00127     if flag:
00128       command.append(flag)
00129 
00130     if expect_seg_fault:
00131       should_or_not = 'should'
00132     else:
00133       should_or_not = 'should not'
00134 
00135     has_seg_fault = Run(command)
00136 
00137     SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
00138 
00139     msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
00140            (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
00141             should_or_not))
00142     self.assert_(has_seg_fault == expect_seg_fault, msg)
00143 
00144   def testDefaultBehavior(self):
00145     """Tests the behavior of the default mode."""
00146 
00147     self.RunAndVerify(env_var_value=None,
00148                       flag_value=None,
00149                       expect_seg_fault=0)
00150 
00151   def testEnvVar(self):
00152     """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
00153 
00154     self.RunAndVerify(env_var_value='0',
00155                       flag_value=None,
00156                       expect_seg_fault=0)
00157     self.RunAndVerify(env_var_value='1',
00158                       flag_value=None,
00159                       expect_seg_fault=1)
00160 
00161   def testFlag(self):
00162     """Tests using the --gtest_break_on_failure flag."""
00163 
00164     self.RunAndVerify(env_var_value=None,
00165                       flag_value='0',
00166                       expect_seg_fault=0)
00167     self.RunAndVerify(env_var_value=None,
00168                       flag_value='1',
00169                       expect_seg_fault=1)
00170 
00171   def testFlagOverridesEnvVar(self):
00172     """Tests that the flag overrides the environment variable."""
00173 
00174     self.RunAndVerify(env_var_value='0',
00175                       flag_value='0',
00176                       expect_seg_fault=0)
00177     self.RunAndVerify(env_var_value='0',
00178                       flag_value='1',
00179                       expect_seg_fault=1)
00180     self.RunAndVerify(env_var_value='1',
00181                       flag_value='0',
00182                       expect_seg_fault=0)
00183     self.RunAndVerify(env_var_value='1',
00184                       flag_value='1',
00185                       expect_seg_fault=1)
00186 
00187   def testBreakOnFailureOverridesThrowOnFailure(self):
00188     """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
00189 
00190     SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
00191     try:
00192       self.RunAndVerify(env_var_value=None,
00193                         flag_value='1',
00194                         expect_seg_fault=1)
00195     finally:
00196       SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
00197 
00198   if IS_WINDOWS:
00199     def testCatchExceptionsDoesNotInterfere(self):
00200       """Tests that gtest_catch_exceptions doesn't interfere."""
00201 
00202       SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
00203       try:
00204         self.RunAndVerify(env_var_value='1',
00205                           flag_value='1',
00206                           expect_seg_fault=1)
00207       finally:
00208         SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
00209 
00210 
00211 if __name__ == '__main__':
00212   gtest_test_utils.Main()


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04