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.
16 import coverage_posix
as coverage
22 class CoveragePosixTest(unittest
.TestCase
):
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.
36 tests: the parsed tests from a Coverage object.
38 for simple_test_name
in ('zippy_tests', 'base_unittests'):
41 if simple_test_name
in item
:
44 self
.assertTrue(found
)
45 for not_test_name
in ('kablammo', 'not_a_unittest'):
48 if not_test_name
in item
:
51 self
.assertFalse(found
)
54 """Setup and process arg parsing."""
55 self
.parser
= coverage
.CoverageOptionParser()
56 (self
.options
, self
.args
) = self
.parser
.parse_args()
57 self
.options
.directory
= '.'
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
)
70 retcode
= c
.Run([sys
.executable
, '-u', '-c',
71 'import sys; sys.exit(%d)' % code
],
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.
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'
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
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'
103 self
.assertRaises(Exception,
105 [sys
.executable
, '-u', '-c', slowscript
])
107 def testFindTestsAsArgs(self
):
108 """Test finding of tests passed as args."""
110 self
.args
+= self
.sample_test_names
111 c
= coverage
.Coverage(self
.options
, self
.args
)
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
))
121 self
.options
.bundles
= filename
122 c
= coverage
.Coverage(self
.options
, self
.args
)
124 self
.confirmSampleTestsArePresent(c
.tests
)
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':
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__':