Merge lp:~dobey/ubuntuone-dev-tools/add-coverage into lp:ubuntuone-dev-tools

Proposed by dobey on 2010-11-22
Status: Merged
Approved by: Natalia Bidart on 2010-11-22
Approved revision: 10
Merged at revision: 10
Proposed branch: lp:~dobey/ubuntuone-dev-tools/add-coverage
Merge into: lp:ubuntuone-dev-tools
Diff against target: 115 lines (+34/-18)
2 files modified
bin/u1trial (+31/-16)
run-tests (+3/-2)
To merge this branch: bzr merge lp:~dobey/ubuntuone-dev-tools/add-coverage
Reviewer Review Type Date Requested Status
Natalia Bidart Approve on 2010-11-22
Eric Casteleijn (community) 2010-11-22 Approve on 2010-11-22
Review via email: mp+41503@code.launchpad.net

Commit Message

Add an option to enable coverage reporting for tests

To post a comment you must log in.
Eric Casteleijn (thisfred) wrote :

Looks great to me!

review: Approve
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/u1trial'
2--- bin/u1trial 2010-11-03 21:04:06 +0000
3+++ bin/u1trial 2010-11-22 19:18:53 +0000
4@@ -20,6 +20,7 @@
5
6 """Test runner that uses a private dbus session and glib main loop."""
7
8+import coverage
9 import gc
10 import inspect
11 import os
12@@ -57,6 +58,7 @@
13 workingDirectory=working_dir,
14 forceGarbageCollection=True)
15 self.required_services = []
16+ self.source_files = []
17
18 def _load_unittest(self, relpath):
19 """Load unit tests from a Python module with the given 'relpath'."""
20@@ -117,29 +119,35 @@
21 # pylint: disable=W0612
22 for root, dirs, files in os.walk(path):
23 for test in files:
24- path = os.path.join(root, test)
25- if test.endswith(".py") and test.startswith("test_"):
26- module_suite = self._load_unittest(path)
27- if pattern:
28- for inner_suite in module_suite._tests:
29- for test in inner_suite._tests:
30- if pattern.match(test.id()):
31- suite.addTest(test)
32- else:
33- suite.addTests(module_suite)
34+ filepath = os.path.join(root, test)
35+ if test.endswith(".py"):
36+ self.source_files.append(filepath)
37+ if test.startswith("test_"):
38+ module_suite = self._load_unittest(filepath)
39+ if pattern:
40+ for inner_suite in module_suite._tests:
41+ for test in inner_suite._tests:
42+ if pattern.match(test.id()):
43+ suite.addTest(test)
44+ else:
45+ suite.addTests(module_suite)
46 return suite
47
48 # pylint: disable=E0202
49- def run(self, path, test_pattern=None, loops=None):
50- """run the tests. """
51+ def run(self, path, options=None):
52+ """run the tests."""
53 success = 0
54 running_services = []
55+ if options.coverage:
56+ coverage.erase()
57+ coverage.start()
58+
59 try:
60- suite = self._collect_tests(path, test_pattern)
61- if loops:
62+ suite = self._collect_tests(path, options.test)
63+ if options.loops:
64 old_suite = suite
65 suite = unittest.TestSuite()
66- for _ in xrange(loops):
67+ for _ in xrange(options.loops):
68 suite.addTest(old_suite)
69
70 # Start any required services
71@@ -155,6 +163,11 @@
72 for runner in running_services:
73 runner.stop_service()
74
75+ if options.coverage:
76+ coverage.stop()
77+ coverage.report(self.source_files, ignore_errors=True,
78+ show_missing=False)
79+
80 if not success:
81 sys.exit(1)
82 else:
83@@ -171,6 +184,8 @@
84 parser.add_option("-l", "--loop", dest="loops", type="int", default=1,
85 help = "loop selected tests LOOPS number of times",
86 metavar="LOOPS")
87+ parser.add_option("-c", "--coverage", action="store_true", dest="coverage",
88+ help="print a coverage report when finished")
89
90 (options, args) = parser.parse_args()
91 if args:
92@@ -182,7 +197,7 @@
93 parser.print_help()
94 sys.exit(2)
95
96- TestRunner().run(testpath, options.test, options.loops)
97+ TestRunner().run(testpath, options)
98
99 if __name__ == '__main__':
100 main()
101
102=== modified file 'run-tests'
103--- run-tests 2010-11-04 15:00:56 +0000
104+++ run-tests 2010-11-22 19:18:53 +0000
105@@ -16,7 +16,8 @@
106 # with this program. If not, see <http://www.gnu.org/licenses/>.
107 set -e
108
109-bin/u1trial ubuntuone
110-bin/u1lint .
111+bin/u1trial -c ubuntuone
112+bin/u1lint
113 pep8 --repeat .
114 rm -rf _trial_temp
115+rm -rf .coverage

Subscribers

People subscribed via source and target branches

to all changes: