Fix build break
[chromium-blink-merge.git] / tools / code_coverage / coverage_posix_unittest.py
blob3164cd3359ae7b8b31f1f645febe0ac420144e29
1 #!/usr/bin/env python
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Unit tests for coverage_posix.py.
8 Run a single test with a command such as:
9 ./coverage_posix_unittest.py CoveragePosixTest.testFindTestsAsArgs
11 Waring that running a single test like that may interfere with the arg
12 parsing tests, since coverage_posix.py uses optparse.OptionParser()
13 which references globals.
14 """
16 import coverage_posix as coverage
17 import os
18 import sys
19 import tempfile
20 import unittest
22 class CoveragePosixTest(unittest.TestCase):
25 def setUp(self):
26 self.parseArgs()
27 self.sample_test_names = ['zippy_tests', '../base/base.gyp:base_unittests']
29 def confirmSampleTestsArePresent(self, tests):
30 """Confirm the tests in self.sample_test_names are in some form in 'tests'.
32 The Coverage object can munge them (e.g. add .exe to the end as needed.
33 Helper function for arg parsing, bundle file tests.
35 Args:
36 tests: the parsed tests from a Coverage object.
37 """
38 for simple_test_name in ('zippy_tests', 'base_unittests'):
39 found = False
40 for item in tests:
41 if simple_test_name in item:
42 found = True
43 break
44 self.assertTrue(found)
45 for not_test_name in ('kablammo', 'not_a_unittest'):
46 found = False
47 for item in tests:
48 if not_test_name in item:
49 found = True
50 break
51 self.assertFalse(found)
53 def parseArgs(self):
54 """Setup and process arg parsing."""
55 self.parser = coverage.CoverageOptionParser()
56 (self.options, self.args) = self.parser.parse_args()
57 self.options.directory = '.'
59 def testSanity(self):
60 """Sanity check we're able to actually run the tests.
62 Simply creating a Coverage instance checks a few things (e.g. on
63 Windows that the coverage tools can be found)."""
64 c = coverage.Coverage(self.options, self.args)
66 def testRunBasicProcess(self):
67 """Test a simple run of a subprocess."""
68 c = coverage.Coverage(self.options, self.args)
69 for code in range(2):
70 retcode = c.Run([sys.executable, '-u', '-c',
71 'import sys; sys.exit(%d)' % code],
72 ignore_error=True)
73 self.assertEqual(code, retcode)
75 def testRunSlowProcess(self):
76 """Test program which prints slowly but doesn't hit our timeout.
78 Overall runtime is longer than the timeout but output lines
79 trickle in keeping things alive.
80 """
81 self.options.timeout = 2.5
82 c = coverage.Coverage(self.options, self.args)
83 slowscript = ('import sys, time\n'
84 'for x in range(10):\n'
85 ' time.sleep(0.5)\n'
86 ' print "hi mom"\n'
87 'sys.exit(0)\n')
88 retcode = c.Run([sys.executable, '-u', '-c', slowscript])
89 self.assertEqual(0, retcode)
91 def testRunExcessivelySlowProcess(self):
92 """Test program which DOES hit our timeout.
94 Initial lines should print but quickly it takes too long and
95 should be killed.
96 """
97 self.options.timeout = 2.5
98 c = coverage.Coverage(self.options, self.args)
99 slowscript = ('import time\n'
100 'for x in range(1,10):\n'
101 ' print "sleeping for %d" % x\n'
102 ' time.sleep(x)\n')
103 self.assertRaises(Exception,
104 c.Run,
105 [sys.executable, '-u', '-c', slowscript])
107 def testFindTestsAsArgs(self):
108 """Test finding of tests passed as args."""
109 self.args += '--'
110 self.args += self.sample_test_names
111 c = coverage.Coverage(self.options, self.args)
112 c.FindTests()
113 self.confirmSampleTestsArePresent(c.tests)
115 def testFindTestsFromBundleFile(self):
116 """Test finding of tests from a bundlefile."""
117 (fd, filename) = tempfile.mkstemp()
118 f = os.fdopen(fd, 'w')
119 f.write(str(self.sample_test_names))
120 f.close()
121 self.options.bundles = filename
122 c = coverage.Coverage(self.options, self.args)
123 c.FindTests()
124 self.confirmSampleTestsArePresent(c.tests)
125 os.unlink(filename)
127 def testExclusionList(self):
128 """Test the gtest_filter exclusion list."""
129 c = coverage.Coverage(self.options, self.args)
130 self.assertFalse(c.GtestFilter('doesnotexist_test'))
131 fake_exclusions = { sys.platform: { 'foobar':
132 ('a','b'),
133 'doesnotexist_test':
134 ('Evil.Crash','Naughty.Test') } }
135 self.assertFalse(c.GtestFilter('barfoo'))
136 filter = c.GtestFilter('doesnotexist_test', fake_exclusions)
137 self.assertEquals('--gtest_filter=-Evil.Crash:-Naughty.Test', filter)
141 if __name__ == '__main__':
142 unittest.main()