1#!/usr/bin/env vpython3 2 3# Copyright 2021 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import argparse 8import json 9import os 10from pyfakefs import fake_filesystem_unittest 11import sys 12import tempfile 13import unittest 14 15from test_results import TestResult 16 17import main_program 18 19 20class FakeTestExecutableWrapper: 21 def __init__(self, hardcoded_test_list, hardcoded_test_results): 22 self._hardcoded_test_list = hardcoded_test_list 23 self._hardcoded_test_results = hardcoded_test_results 24 25 def list_all_tests(self): 26 return self._hardcoded_test_list 27 28 def run_tests(self, list_of_tests_to_run): 29 results = [] 30 for test in self._hardcoded_test_results: 31 if test.test_name in list_of_tests_to_run: 32 results.append(test) 33 return results 34 35 36class EndToEndTests(fake_filesystem_unittest.TestCase): 37 def test_basic_scenario(self): 38 with tempfile.TemporaryDirectory() as tmpdirname: 39 # Prepare simulated inputs. 40 test_list = [ 41 'test_foo', 'test_bar', 'test_foobar', 'module/test_foo' 42 ] 43 test_results = [ 44 TestResult('test_foo', 'PASS'), 45 TestResult('test_bar', 'PASS'), 46 TestResult('test_foobar', 'FAILED'), 47 TestResult('module/test_foo', 'PASS') 48 ] 49 fake_executable_wrapper = FakeTestExecutableWrapper( 50 test_list, test_results) 51 parser = argparse.ArgumentParser() 52 main_program.add_cmdline_args(parser) 53 output_file = os.path.join(tmpdirname, 'test.out') 54 args = parser.parse_args( 55 args=['--isolated-script-test-output={}'.format(output_file)]) 56 fake_env = {'GTEST_SHARD_INDEX': 0, 'GTEST_TOTAL_SHARDS': 1} 57 58 # Run code under test. 59 main_program.main([fake_executable_wrapper], args, fake_env) 60 61 # Verify results. 62 with open(output_file) as f: 63 actual_json_output = json.load(f) 64 del actual_json_output['seconds_since_epoch'] 65 # yapf: disable 66 expected_json_output = { 67 'interrupted': False, 68 'path_delimiter': '//', 69 #'seconds_since_epoch': 1635974313.8388052, 70 'version': 3, 71 'tests': { 72 'test_foo': { 73 'expected': 'PASS', 74 'actual': 'PASS' 75 }, 76 'test_bar': { 77 'expected': 'PASS', 78 'actual': 'PASS' 79 }, 80 'test_foobar': { 81 'expected': 'PASS', 82 'actual': 'FAILED' 83 }, 84 'module/test_foo': { 85 'expected': 'PASS', 86 'actual': 'PASS' 87 }}, 88 'num_failures_by_type': { 89 'PASS': 3, 90 'FAILED': 1 91 } 92 } 93 # yapf: enable 94 self.assertEqual(actual_json_output, expected_json_output) 95 96 97if __name__ == '__main__': 98 unittest.main() 99