OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import re | 8 import re |
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
11 import unittest | 11 import unittest |
12 | 12 |
13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
14 | 14 |
15 sys.path.append(os.path.join(ROOT_DIR, 'data', 'gtest_fake')) | 15 sys.path.append(os.path.join(ROOT_DIR, 'data', 'gtest_fake')) |
16 import gtest_fake | 16 import gtest_fake_base |
| 17 |
| 18 |
| 19 def RunTest(test_file): |
| 20 target = os.path.join(ROOT_DIR, 'data', 'gtest_fake', test_file) |
| 21 cmd = [ |
| 22 sys.executable, |
| 23 os.path.join(ROOT_DIR, 'run_test_cases.py'), |
| 24 '--no-dump', |
| 25 target, |
| 26 ] |
| 27 logging.debug(' '.join(cmd)) |
| 28 proc = subprocess.Popen( |
| 29 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 30 # pylint is confused. |
| 31 out, err = proc.communicate() or ('', '') |
| 32 |
| 33 return (out, err, proc.returncode) |
17 | 34 |
18 | 35 |
19 class TraceTestCases(unittest.TestCase): | 36 class TraceTestCases(unittest.TestCase): |
20 def setUp(self): | 37 def setUp(self): |
21 # Make sure there's no environment variable that could do side effects. | 38 # Make sure there's no environment variable that could do side effects. |
22 os.environ.pop('GTEST_SHARD_INDEX', '') | 39 os.environ.pop('GTEST_SHARD_INDEX', '') |
23 os.environ.pop('GTEST_TOTAL_SHARDS', '') | 40 os.environ.pop('GTEST_TOTAL_SHARDS', '') |
24 | 41 |
25 def test_simple(self): | 42 def _check_results(self, expected_out_re, out, err): |
26 target = os.path.join(ROOT_DIR, 'data', 'gtest_fake', 'gtest_fake.py') | |
27 cmd = [ | |
28 sys.executable, | |
29 os.path.join(ROOT_DIR, 'run_test_cases.py'), | |
30 '--no-dump', | |
31 target, | |
32 ] | |
33 logging.debug(' '.join(cmd)) | |
34 proc = subprocess.Popen( | |
35 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
36 # pylint is confused. | |
37 out, err = proc.communicate() or ('', '') | |
38 self.assertEquals(0, proc.returncode) | |
39 if sys.platform == 'win32': | 43 if sys.platform == 'win32': |
40 out = out.replace('\r\n', '\n') | 44 out = out.replace('\r\n', '\n') |
41 lines = out.splitlines() | 45 lines = out.splitlines() |
| 46 |
| 47 for index in range(len(expected_out_re)): |
| 48 line = lines.pop(0) |
| 49 self.assertTrue( |
| 50 re.match('^%s$' % expected_out_re[index], line), |
| 51 (index, expected_out_re[index], repr(line))) |
| 52 self.assertEquals([], lines) |
| 53 self.assertEquals('', err) |
| 54 |
| 55 def test_simple_pass(self): |
| 56 out, err, return_code = RunTest('gtest_fake_pass.py') |
| 57 |
| 58 self.assertEquals(0, return_code) |
| 59 |
42 expected_out_re = [ | 60 expected_out_re = [ |
43 r'\[\d/\d\] \d\.\d\ds .+', | 61 r'\[\d/\d\] \d\.\d\ds .+', |
44 r'\[\d/\d\] \d\.\d\ds .+', | 62 r'\[\d/\d\] \d\.\d\ds .+', |
| 63 r'\[\d/\d\] \d\.\d\ds .+', |
| 64 re.escape('Success: 3 100.00%'), |
| 65 re.escape('Flaky: 0 0.00%'), |
| 66 re.escape('Fail: 0 0.00%'), |
| 67 r'\d+\.\ds Done running 3 tests with 3 executions. \d+\.\d test/s', |
| 68 ] |
| 69 |
| 70 self._check_results(expected_out_re, out, err) |
| 71 |
| 72 def test_simple_fail(self): |
| 73 out, err, return_code = RunTest('gtest_fake_fail.py') |
| 74 |
| 75 self.assertEquals(1, return_code) |
| 76 |
| 77 expected_out_re = [ |
| 78 r'\[\d/\d\] \d\.\d\ds .+', |
| 79 r'\[\d/\d\] \d\.\d\ds .+', |
45 r'\[\d/\d\] \d\.\d\ds .+', | 80 r'\[\d/\d\] \d\.\d\ds .+', |
46 r'\[\d/\d\] \d\.\d\ds .+', | 81 r'\[\d/\d\] \d\.\d\ds .+', |
47 r'\[\d/\d\] \d\.\d\ds .+', | 82 r'\[\d/\d\] \d\.\d\ds .+', |
48 r'\[\d/\d\] \d\.\d\ds .+', | 83 r'\[\d/\d\] \d\.\d\ds .+', |
49 re.escape('Note: Google Test filter = Baz.Fail'), | 84 re.escape('Note: Google Test filter = Baz.Fail'), |
50 r'', | 85 r'', |
51 ] + [ | 86 ] + [ |
52 re.escape(l) for l in gtest_fake.get_test_output('Baz.Fail').splitlines() | 87 re.escape(l) for l in |
| 88 gtest_fake_base.get_test_output('Baz.Fail').splitlines() |
53 ] + [ | 89 ] + [ |
54 '', | 90 '', |
55 ] + [ | 91 ] + [ |
56 re.escape(l) for l in gtest_fake.get_footer(1).splitlines() | 92 re.escape(l) for l in gtest_fake_base.get_footer(1, 1).splitlines() |
57 ] + [ | 93 ] + [ |
58 '', | 94 '', |
59 re.escape('Success: 3 75.00%'), | 95 re.escape('Success: 3 75.00%'), |
60 re.escape('Flaky: 0 0.00%'), | 96 re.escape('Flaky: 0 0.00%'), |
61 re.escape('Fail: 1 25.00%'), | 97 re.escape('Fail: 1 25.00%'), |
62 r'\d+\.\ds Done running 4 tests with 6 executions. \d+\.\d test/s', | 98 r'\d+\.\ds Done running 4 tests with 6 executions. \d+\.\d test/s', |
63 ] | 99 ] |
64 for index in range(len(expected_out_re)): | 100 self._check_results(expected_out_re, out, err) |
65 line = lines.pop(0) | |
66 self.assertTrue( | |
67 re.match('^%s$' % expected_out_re[index], line), | |
68 (index, expected_out_re[index], repr(line))) | |
69 self.assertEquals([], lines) | |
70 self.assertEquals('', err) | |
71 | 101 |
72 | 102 |
73 if __name__ == '__main__': | 103 if __name__ == '__main__': |
74 VERBOSE = '-v' in sys.argv | 104 VERBOSE = '-v' in sys.argv |
75 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | 105 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) |
76 unittest.main() | 106 unittest.main() |
OLD | NEW |