Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """Tests Google Test's throw-on-failure mode with exceptions disabled.
00033
00034 This script invokes gtest_throw_on_failure_test_ (a program written with
00035 Google Test) with different environments and command line flags.
00036 """
00037
00038 __author__ = 'wan@google.com (Zhanyong Wan)'
00039
00040 import os
00041 import gtest_test_utils
00042
00043
00044
00045
00046
00047 THROW_ON_FAILURE = 'gtest_throw_on_failure'
00048
00049
00050
00051 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00052 'gtest_throw_on_failure_test_')
00053
00054
00055
00056
00057
00058 def SetEnvVar(env_var, value):
00059 """Sets an environment variable to a given value; unsets it when the
00060 given value is None.
00061 """
00062
00063 env_var = env_var.upper()
00064 if value is not None:
00065 os.environ[env_var] = value
00066 elif env_var in os.environ:
00067 del os.environ[env_var]
00068
00069
00070 def Run(command):
00071 """Runs a command; returns True/False if its exit code is/isn't 0."""
00072
00073 print 'Running "%s". . .' % ' '.join(command)
00074 p = gtest_test_utils.Subprocess(command)
00075 return p.exited and p.exit_code == 0
00076
00077
00078
00079
00080 class ThrowOnFailureTest(gtest_test_utils.TestCase):
00081 """Tests the throw-on-failure mode."""
00082
00083 def RunAndVerify(self, env_var_value, flag_value, should_fail):
00084 """Runs gtest_throw_on_failure_test_ and verifies that it does
00085 (or does not) exit with a non-zero code.
00086
00087 Args:
00088 env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
00089 variable; None if the variable should be unset.
00090 flag_value: value of the --gtest_break_on_failure flag;
00091 None if the flag should not be present.
00092 should_fail: True iff the program is expected to fail.
00093 """
00094
00095 SetEnvVar(THROW_ON_FAILURE, env_var_value)
00096
00097 if env_var_value is None:
00098 env_var_value_msg = ' is not set'
00099 else:
00100 env_var_value_msg = '=' + env_var_value
00101
00102 if flag_value is None:
00103 flag = ''
00104 elif flag_value == '0':
00105 flag = '--%s=0' % THROW_ON_FAILURE
00106 else:
00107 flag = '--%s' % THROW_ON_FAILURE
00108
00109 command = [EXE_PATH]
00110 if flag:
00111 command.append(flag)
00112
00113 if should_fail:
00114 should_or_not = 'should'
00115 else:
00116 should_or_not = 'should not'
00117
00118 failed = not Run(command)
00119
00120 SetEnvVar(THROW_ON_FAILURE, None)
00121
00122 msg = ('when %s%s, an assertion failure in "%s" %s cause a non-zero '
00123 'exit code.' %
00124 (THROW_ON_FAILURE, env_var_value_msg, ' '.join(command),
00125 should_or_not))
00126 self.assert_(failed == should_fail, msg)
00127
00128 def testDefaultBehavior(self):
00129 """Tests the behavior of the default mode."""
00130
00131 self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False)
00132
00133 def testThrowOnFailureEnvVar(self):
00134 """Tests using the GTEST_THROW_ON_FAILURE environment variable."""
00135
00136 self.RunAndVerify(env_var_value='0',
00137 flag_value=None,
00138 should_fail=False)
00139 self.RunAndVerify(env_var_value='1',
00140 flag_value=None,
00141 should_fail=True)
00142
00143 def testThrowOnFailureFlag(self):
00144 """Tests using the --gtest_throw_on_failure flag."""
00145
00146 self.RunAndVerify(env_var_value=None,
00147 flag_value='0',
00148 should_fail=False)
00149 self.RunAndVerify(env_var_value=None,
00150 flag_value='1',
00151 should_fail=True)
00152
00153 def testThrowOnFailureFlagOverridesEnvVar(self):
00154 """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE."""
00155
00156 self.RunAndVerify(env_var_value='0',
00157 flag_value='0',
00158 should_fail=False)
00159 self.RunAndVerify(env_var_value='0',
00160 flag_value='1',
00161 should_fail=True)
00162 self.RunAndVerify(env_var_value='1',
00163 flag_value='0',
00164 should_fail=False)
00165 self.RunAndVerify(env_var_value='1',
00166 flag_value='1',
00167 should_fail=True)
00168
00169
00170 if __name__ == '__main__':
00171 gtest_test_utils.Main()