def _TestOutFile(self, test_name, expected): gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name) command = [ gtest_prog_path, '--gtest_output=json:%s' % self.output_dir_ ] p = gtest_test_utils.Subprocess( command, working_dir=gtest_test_utils.GetTempDir()) self.assert_(p.exited) self.assertEquals(0, p.exit_code) output_file_name1 = test_name + '.json' output_file1 = os.path.join(self.output_dir_, output_file_name1) output_file_name2 = 'lt-' + output_file_name1 output_file2 = os.path.join(self.output_dir_, output_file_name2) self.assert_( os.path.isfile(output_file1) or os.path.isfile(output_file2), output_file1) if os.path.isfile(output_file1): with open(output_file1) as f: actual = json.load(f) else: with open(output_file2) as f: actual = json.load(f) self.assertEqual(expected, gtest_json_test_utils.normalize(actual))
def _TestOutFile(self, test_name, expected_xml): gtest_prog_path = gtest_test_utils.GetTestExecutablePath(test_name) command = [gtest_prog_path, "--gtest_output=xml:%s" % self.output_dir_] p = gtest_test_utils.Subprocess( command, working_dir=gtest_test_utils.GetTempDir()) self.assert_(p.exited) self.assertEquals(0, p.exit_code) output_file_name1 = test_name + ".xml" output_file1 = os.path.join(self.output_dir_, output_file_name1) output_file_name2 = 'lt-' + output_file_name1 output_file2 = os.path.join(self.output_dir_, output_file_name2) self.assert_( os.path.isfile(output_file1) or os.path.isfile(output_file2), output_file1) expected = minidom.parseString(expected_xml) if os.path.isfile(output_file1): actual = minidom.parse(output_file1) else: actual = minidom.parse(output_file2) self.NormalizeXml(actual.documentElement) self.AssertEquivalentNodes(expected.documentElement, actual.documentElement) expected.unlink() actual.unlink()
def testSuppressedJsonOutput(self): """Verifies that no JSON output is generated. Tests that no JSON file is generated if the default JSON listener is shut down before RUN_ALL_TESTS is invoked. """ json_path = os.path.join(gtest_test_utils.GetTempDir(), GTEST_PROGRAM_NAME + 'out.json') if os.path.isfile(json_path): os.remove(json_path) command = [GTEST_PROGRAM_PATH, '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path), '--shut_down_xml'] p = gtest_test_utils.Subprocess(command) if p.terminated_by_signal: # p.signal is available only if p.terminated_by_signal is True. self.assertFalse( p.terminated_by_signal, '%s was killed by signal %d' % (GTEST_PROGRAM_NAME, p.signal)) else: self.assert_(p.exited) self.assertEquals(1, p.exit_code, "'%s' exited with code %s, which doesn't match " 'the expected exit code %s.' % (command, p.exit_code, 1)) self.assert_(not os.path.isfile(json_path))
def _GetJsonOutput(self, gtest_prog_name, extra_args, expected_exit_code): """Returns the JSON output generated by running the program gtest_prog_name. Furthermore, the program's exit code must be expected_exit_code. Args: gtest_prog_name: Google Test binary name. extra_args: extra arguments to binary invocation. expected_exit_code: program's exit code. """ json_path = os.path.join(gtest_test_utils.GetTempDir(), gtest_prog_name + 'out.json') gtest_prog_path = gtest_test_utils.GetTestExecutablePath(gtest_prog_name) command = ( [gtest_prog_path, '%s=json:%s' % (GTEST_OUTPUT_FLAG, json_path)] + extra_args ) p = gtest_test_utils.Subprocess(command) if p.terminated_by_signal: self.assert_(False, '%s was killed by signal %d' % (gtest_prog_name, p.signal)) else: self.assert_(p.exited) self.assertEquals(expected_exit_code, p.exit_code, "'%s' exited with code %s, which doesn't match " 'the expected exit code %s.' % (command, p.exit_code, expected_exit_code)) with open(json_path) as f: actual = json.load(f) return actual
def RunAndReturnOutput(args=None): """Runs the test program and returns its output.""" return gtest_test_utils.Subprocess([ gtest_test_utils.GetTestExecutablePath( 'googletest-global-environment-unittest_') ] + (args or [])).output
def _GetXmlOutput(self, gtest_prog_name, extra_args, extra_env, expected_exit_code): """ Returns the xml output generated by running the program gtest_prog_name. Furthermore, the program's exit code must be expected_exit_code. """ xml_path = os.path.join(gtest_test_utils.GetTempDir(), gtest_prog_name + 'out.xml') gtest_prog_path = gtest_test_utils.GetTestExecutablePath( gtest_prog_name) command = ( [gtest_prog_path, '%s=xml:%s' % (GTEST_OUTPUT_FLAG, xml_path)] + extra_args) environ_copy = os.environ.copy() if extra_env: environ_copy.update(extra_env) p = gtest_test_utils.Subprocess(command, env=environ_copy) if p.terminated_by_signal: self.assert_( False, '%s was killed by signal %d' % (gtest_prog_name, p.signal)) else: self.assert_(p.exited) self.assertEquals( expected_exit_code, p.exit_code, "'%s' exited with code %s, which doesn't match " 'the expected exit code %s.' % (command, p.exit_code, expected_exit_code)) actual = minidom.parse(xml_path) return actual
def GetFlag(flag): """Runs googletest-env-var-test_ and returns its output.""" args = [COMMAND] if flag is not None: args += [flag] return gtest_test_utils.Subprocess(args, env=environ).output
def Run(command): """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise.""" p = gtest_test_utils.Subprocess(command, env=environ) if p.terminated_by_signal: return 1 else: return 0
def TestExitCodeAndOutput(command): """Runs the given command and verifies its exit code and output.""" # Verifies that 'command' exits with code 1. p = gtest_test_utils.Subprocess(command) if p.exited and p.exit_code == 0: Assert('IMPORTANT NOTICE' in p.output) Assert('InitGoogleTest' in p.output)
def RunAndReturnOutput(extra_env, args): """Runs the test program and returns its output.""" environ_copy = os.environ.copy() environ_copy.update(extra_env) return gtest_test_utils.Subprocess([COMMAND] + args, env=environ_copy).output
def testSetupErrorAndTearDownError(self): p = gtest_test_utils.Subprocess(COMMAND) self.assertNotEqual(p.exit_code, 0, msg=p.output) self.assertIn( '[ FAILED ] SetupFailTest: SetUpTestSuite or TearDownTestSuite\n' '[ FAILED ] TearDownFailTest: SetUpTestSuite or TearDownTestSuite\n' '\n' ' 2 FAILED TEST SUITES\n', p.output)
def RunAndExtractDisabledBannerList(args=None): """Runs the test program and returns tests that printed a disabled banner.""" p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ) banners_printed = [] for line in p.output.split('\n'): match = DISABLED_BANNER_REGEX.match(line) if match is not None: banners_printed.append(match.group(1)) return banners_printed
def TestExitCodeAndOutput(command): """Runs the given command and verifies its exit code and output.""" err = ('Duplicate parameterized test name \'a\'') p = gtest_test_utils.Subprocess(command) Assert(p.terminated_by_signal) # Check for appropriate output Assert(err in p.output)
def TestExitCodeAndOutput(command): """Runs the given command and verifies its exit code and output.""" err = ('Parameterized test name \'"InvalidWithQuotes"\' is invalid') p = gtest_test_utils.Subprocess(command) Assert(p.terminated_by_signal) # Verify the output message contains appropriate output Assert(err in p.output)
def testUnhandledCxxExceptionsAbortTheProgram(self): # Filters out SEH exception tests on Windows. Unhandled SEH exceptions # cause tests to show pop-up windows there. FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*' # By default, Google Test doesn't catch the exceptions. uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess( [EX_EXE_PATH, NO_CATCH_EXCEPTIONS_FLAG, FITLER_OUT_SEH_TESTS_FLAG], env=environ).output self.assert_('Unhandled C++ exception terminating the program' in uncaught_exceptions_ex_binary_output) self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
def UsesColor(term, color_env_var, color_flag): """Runs googletest-color-test_ and returns its exit code.""" SetEnvVar('TERM', term) SetEnvVar(COLOR_ENV_VAR, color_env_var) if color_flag is None: args = [] else: args = ['--%s=%s' % (COLOR_FLAG, color_flag)] p = gtest_test_utils.Subprocess([COMMAND] + args) return not p.exited or p.exit_code
def testTestExecutionIsFiltered(self): """Tests that the test filter is picked up from the testbridge env var.""" subprocess_env = os.environ.copy() subprocess_env[TESTBRIDGE_NAME] = '*.TestThatSucceeds' p = gtest_test_utils.Subprocess(COMMAND, env=subprocess_env) self.assertEquals(0, p.exit_code) Assert('filter = *.TestThatSucceeds' in p.output) Assert('[ OK ] TestFilterTest.TestThatSucceeds' in p.output) Assert('[ PASSED ] 1 test.' in p.output)
def RunWithFlag(flag): """Runs gtest_help_test_ with the given flag. Returns: the exit code and the text output as a tuple. Args: flag: the command-line flag to pass to gtest_help_test_, or None. """ if flag is None: command = [PROGRAM_PATH] else: command = [PROGRAM_PATH, flag] child = gtest_test_utils.Subprocess(command) return child.exit_code, child.output
def RunAndExtractTestList(args=None): """Runs the test program and returns its exit code and a list of tests run.""" p = gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ) tests_run = [] test_case = '' test = '' for line in p.output.split('\n'): match = TEST_CASE_REGEX.match(line) if match is not None: test_case = match.group(1) else: match = TEST_REGEX.match(line) if match is not None: test = match.group(1) tests_run.append(test_case + '.' + test) return (tests_run, p.exit_code)
def GetShellCommandOutput(env_cmd): """Runs a command in a sub-process, and returns its output in a string. Args: env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra environment variables to set, and element 1 is a string with the command and any flags. Returns: A string with the command's combined standard and diagnostic output. """ # Spawns cmd in a sub-process, and gets its standard I/O file objects. # Set and save the environment properly. environ = os.environ.copy() environ.update(env_cmd[0]) p = gtest_test_utils.Subprocess(env_cmd[1], env=environ) return p.output
def RunAndReturnOutput(test_suite=None, fail_fast=None, run_disabled=False): """Runs the test program and returns its output.""" args = [] xml_path = os.path.join(gtest_test_utils.GetTempDir(), '.GTestFailFastUnitTest.xml') args += ['--gtest_output=xml:' + xml_path] if fail_fast is not None: if isinstance(fail_fast, str): args += ['--%s=%s' % (FAIL_FAST_FLAG, fail_fast)] elif fail_fast: args += ['--%s' % FAIL_FAST_FLAG] else: args += ['--no%s' % FAIL_FAST_FLAG] if test_suite: args += ['--%s=%s.*' % (FILTER_FLAG, test_suite)] if run_disabled: args += ['--%s' % RUN_DISABLED_FLAG] txt_out = gtest_test_utils.Subprocess([COMMAND] + args, env=environ).output with open(xml_path) as xml_file: return txt_out, xml_file.read()
def testDefaultOutputFile(self): """ Confirms that Google Test produces an XML output file with the expected default name if no name is explicitly specified. """ output_file = os.path.join(gtest_test_utils.GetTempDir(), GTEST_DEFAULT_OUTPUT_FILE) gtest_prog_path = gtest_test_utils.GetTestExecutablePath( 'gtest_no_test_unittest') try: os.remove(output_file) except OSError: e = sys.exc_info()[1] if e.errno != errno.ENOENT: raise p = gtest_test_utils.Subprocess( [gtest_prog_path, '%s=xml' % GTEST_OUTPUT_FLAG], working_dir=gtest_test_utils.GetTempDir()) self.assert_(p.exited) self.assertEquals(0, p.exit_code) self.assert_(os.path.isfile(output_file))
def _GetOutput(self, out_format): file_path = os.path.join(gtest_test_utils.GetTempDir(), 'test_out.' + out_format) gtest_prog_path = gtest_test_utils.GetTestExecutablePath( 'gtest_list_output_unittest_') command = ([ gtest_prog_path, '%s=%s:%s' % (GTEST_OUTPUT_FLAG, out_format, file_path), '--gtest_list_tests' ]) environ_copy = os.environ.copy() p = gtest_test_utils.Subprocess( command, env=environ_copy, working_dir=gtest_test_utils.GetTempDir()) self.assertTrue(p.exited) self.assertEqual(0, p.exit_code) self.assertTrue(os.path.isfile(file_path)) with open(file_path) as f: result = f.read() return result
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """Tests Google Test's gtest skip in environment setup behavior. This script invokes gtest_skip_in_environment_setup_test_ and verifies its output. """ import re from googletest.test import gtest_test_utils # Path to the gtest_skip_in_environment_setup_test binary EXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_skip_test') OUTPUT = gtest_test_utils.Subprocess([EXE_PATH]).output # Test. class SkipEntireEnvironmentTest(gtest_test_utils.TestCase): def testSkipEntireEnvironmentTest(self): self.assertIn('Skipped\nskipping single test\n', OUTPUT) skip_fixture = 'Skipped\nskipping all tests for this fixture\n' self.assertIsNotNone( re.search(skip_fixture + '.*' + skip_fixture, OUTPUT, flags=re.DOTALL), repr(OUTPUT)) self.assertNotIn('FAILED', OUTPUT) if __name__ == '__main__':
def Run(args): """Runs googletest-list-tests-unittest_ and returns the list of tests printed.""" return gtest_test_utils.Subprocess([EXE_PATH] + args, capture_stderr=False).output
u'*', u'classname': u'', u'failures': [{ u'failure': u'gtest_no_test_unittest.cc:*\n' u'Expected equality of these values:\n' u' 1\n 2' + STACK_TRACE_TEMPLATE, u'type': u'', }] }] }], } GTEST_PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath(GTEST_PROGRAM_NAME) SUPPORTS_TYPED_TESTS = 'TypedTest' in gtest_test_utils.Subprocess( [GTEST_PROGRAM_PATH, GTEST_LIST_TESTS_FLAG], capture_stderr=False).output class GTestJsonOutputUnitTest(gtest_test_utils.TestCase): """Unit test for Google Test's JSON output functionality. """ # This test currently breaks on platforms that do not support typed and # type-parameterized tests, so we don't run it under them. if SUPPORTS_TYPED_TESTS: def testNonEmptyJsonOutput(self): """Verifies JSON output for a Google Test binary with non-empty output. Runs a test program that generates a non-empty JSON output, and tests that the JSON output is expected.
def RunAndReturnOutput(args=None): """Runs the test program and returns its output.""" return gtest_test_utils.Subprocess([COMMAND] + (args or []), env=environ).output
pass import sys from googletest.test import gtest_test_utils # Constants. # Checks if this platform can pass empty environment variables to child # processes. We set an env variable to an empty string and invoke a python # script in a subprocess to print whether the variable is STILL in # os.environ. We then use 'eval' to parse the child's output so that an # exception is thrown if the input is anything other than 'True' nor 'False'. CAN_PASS_EMPTY_ENV = False if sys.executable: os.environ['EMPTY_VAR'] = '' child = gtest_test_utils.Subprocess([ sys.executable, '-c', 'import os; print(\'EMPTY_VAR\' in os.environ)' ]) CAN_PASS_EMPTY_ENV = eval(child.output) # Check if this platform can unset environment variables in child processes. # We set an env variable to a non-empty string, unset it, and invoke # a python script in a subprocess to print whether the variable # is NO LONGER in os.environ. # We use 'eval' to parse the child's output so that an exception # is thrown if the input is neither 'True' nor 'False'. CAN_UNSET_ENV = False if sys.executable: os.environ['UNSET_VAR'] = 'X' del os.environ['UNSET_VAR'] child = gtest_test_utils.Subprocess([ sys.executable, '-c',
IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU' IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD' IS_WINDOWS = os.name == 'nt' PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_') FLAG_PREFIX = '--gtest_' DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style' STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to' UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing' LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG), re.sub('^--', '/', LIST_TESTS_FLAG), re.sub('_', '-', LIST_TESTS_FLAG)] INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing' SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess( [PROGRAM_PATH, LIST_TESTS_FLAG]).output # The help message must match this regex. HELP_REGEX = re.compile( FLAG_PREFIX + r'list_tests.*' + FLAG_PREFIX + r'filter=.*' + FLAG_PREFIX + r'also_run_disabled_tests.*' + FLAG_PREFIX + r'repeat=.*' + FLAG_PREFIX + r'shuffle.*' + FLAG_PREFIX + r'random_seed=.*' + FLAG_PREFIX + r'color=.*' + FLAG_PREFIX + r'brief.*' + FLAG_PREFIX + r'print_time.*' + FLAG_PREFIX + r'output=.*' + FLAG_PREFIX + r'break_on_failure.*' + FLAG_PREFIX + r'throw_on_failure.*' +
def Run(command): """Runs a command; returns True/False if its exit code is/isn't 0.""" print('Running "%s". . .' % ' '.join(command)) p = gtest_test_utils.Subprocess(command) return p.exited and p.exit_code == 0