@@ -73,10 +73,8 @@ def assertDefectsEqual(self, actual, expected):
7373 'item {}' .format (i ))
7474
7575
76- # Metaclass to allow for parameterized tests
77- class Parameterized (type ):
78-
79- """Provide a test method parameterization facility.
76+ def parameterize (cls ):
77+ """A test method parameterization class decorator.
8078
8179 Parameters are specified as the value of a class attribute that ends with
8280 the string '_params'. Call the portion before '_params' the prefix. Then
@@ -92,9 +90,10 @@ class Parameterized(type):
9290 In a _params dictioanry, the keys become part of the name of the generated
9391 tests. In a _params list, the values in the list are converted into a
9492 string by joining the string values of the elements of the tuple by '_' and
95- converting any blanks into '_'s, and this become part of the name. The
96- full name of a generated test is the portion of the _params name before the
97- '_params' portion, plus an '_', plus the name derived as explained above.
93+ converting any blanks into '_'s, and this become part of the name.
94+ The full name of a generated test is a 'test_' prefix, the portion of the
95+ test function name after the '_as_' separator, plus an '_', plus the name
96+ derived as explained above.
9897
9998 For example, if we have:
10099
@@ -123,30 +122,29 @@ def example_as_myfunc_input(self, name, count):
123122 be used to select the test individually from the unittest command line.
124123
125124 """
126-
127- def __new__ (meta , classname , bases , classdict ):
128- paramdicts = {}
129- for name , attr in classdict .items ():
130- if name .endswith ('_params' ):
131- if not hasattr (attr , 'keys' ):
132- d = {}
133- for x in attr :
134- if not hasattr (x , '__iter__' ):
135- x = (x ,)
136- n = '_' .join (str (v ) for v in x ).replace (' ' , '_' )
137- d [n ] = x
138- attr = d
139- paramdicts [name [:- 7 ] + '_as_' ] = attr
140- testfuncs = {}
141- for name , attr in classdict .items ():
142- for paramsname , paramsdict in paramdicts .items ():
143- if name .startswith (paramsname ):
144- testnameroot = 'test_' + name [len (paramsname ):]
145- for paramname , params in paramsdict .items ():
146- test = (lambda self , name = name , params = params :
147- getattr (self , name )(* params ))
148- testname = testnameroot + '_' + paramname
149- test .__name__ = testname
150- testfuncs [testname ] = test
151- classdict .update (testfuncs )
152- return super ().__new__ (meta , classname , bases , classdict )
125+ paramdicts = {}
126+ for name , attr in cls .__dict__ .items ():
127+ if name .endswith ('_params' ):
128+ if not hasattr (attr , 'keys' ):
129+ d = {}
130+ for x in attr :
131+ if not hasattr (x , '__iter__' ):
132+ x = (x ,)
133+ n = '_' .join (str (v ) for v in x ).replace (' ' , '_' )
134+ d [n ] = x
135+ attr = d
136+ paramdicts [name [:- 7 ] + '_as_' ] = attr
137+ testfuncs = {}
138+ for name , attr in cls .__dict__ .items ():
139+ for paramsname , paramsdict in paramdicts .items ():
140+ if name .startswith (paramsname ):
141+ testnameroot = 'test_' + name [len (paramsname ):]
142+ for paramname , params in paramsdict .items ():
143+ test = (lambda self , name = name , params = params :
144+ getattr (self , name )(* params ))
145+ testname = testnameroot + '_' + paramname
146+ test .__name__ = testname
147+ testfuncs [testname ] = test
148+ for key , value in testfuncs .items ():
149+ setattr (cls , key , value )
150+ return cls
0 commit comments