Skip to content

Commit ffbf9e8

Browse files
committed
Make test categories composable
Summary: Previously the add_test_categories would simply overwrite the current set of categories for a method. This change makes the decorator truly "add" categories, by extending the current set of categories instead of replacing it. To do this, I have: - replaced the getCategories() property on a method (which was itself a method), with a simple list property "categories". This makes add_test_categories easier to implement, and test categories isn't something which should change between calls anyway. - rewritten the getCategoriesForTest function to merge method categories with the categories of the test case. Previously, it would just use the method categories if they were present. I have also greatly simplified this method. Originally, it would use a lot of introspection to enable it being called on various types of objects. Based on my tests, it was only ever being called on a test case. The new function uses much less introspection then the preivous one, so we should easily catch any stray uses, if there are any, as they will generate exceptions now. Reviewers: zturner, tfiala, tberghammer Subscribers: lldb-commits Differential Revision: http://reviews.llvm.org/D15451 llvm-svn: 255493
1 parent b8ea7a0 commit ffbf9e8

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,18 @@ def fn_neq(x,y): return x != y
509509
# Decorators for categorizing test cases.
510510
#
511511
from functools import wraps
512+
512513
def add_test_categories(cat):
513-
"""Decorate an item with test categories"""
514+
"""Add test categories to a TestCase method"""
514515
cat = test_categories.validate(cat, True)
515516
def impl(func):
516-
func.getCategories = lambda test: cat
517+
if isinstance(func, type) and issubclass(func, unittest2.TestCase):
518+
raise Exception("@add_test_categories can only be used to decorate a test method")
519+
if hasattr(func, "categories"):
520+
cat.extend(func.categories)
521+
func.categories = cat
517522
return func
523+
518524
return impl
519525

520526
def benchmarks_test(func):

lldb/packages/Python/lldbsuite/test/test_categories.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from __future__ import absolute_import
6+
from __future__ import print_function
67

78
# System modules
89
import sys

lldb/packages/Python/lldbsuite/test/test_result.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,17 @@ def getDescription(self, test):
9797
else:
9898
return str(test)
9999

100-
def getCategoriesForTest(self,test):
101-
if hasattr(test,"_testMethodName"):
102-
test_method = getattr(test,"_testMethodName")
103-
test_method = getattr(test,test_method)
104-
else:
105-
test_method = None
106-
if test_method != None and hasattr(test_method,"getCategories"):
107-
test_categories = test_method.getCategories(test)
108-
elif hasattr(test,"getCategories"):
109-
test_categories = test.getCategories()
110-
elif inspect.ismethod(test) and test.__self__ != None and hasattr(test.__self__,"getCategories"):
111-
test_categories = test.__self__.getCategories()
112-
else:
113-
test_categories = []
114-
if test_categories == None:
115-
test_categories = []
100+
def getCategoriesForTest(self, test):
101+
"""
102+
Gets all the categories for the currently running test method in test case
103+
"""
104+
test_categories = []
105+
test_method = getattr(test, test._testMethodName)
106+
if test_method != None and hasattr(test_method, "categories"):
107+
test_categories.extend(test_method.categories)
108+
109+
test_categories.extend(test.getCategories())
110+
116111
return test_categories
117112

118113
def hardMarkAsSkipped(self,test):

0 commit comments

Comments
 (0)