165
168
{attribute_name: mod}))
172
def with_scenarios(prefix="test"):
173
"""Mutate the decorated TestCase, producing methods for each scenario.
175
:param str prefix: the prefix to match for test methods
178
def _populate_scenarios(cls):
179
scenarios = getattr(cls, "scenarios", None)
180
if scenarios is None:
184
(name, test) for name, test in inspect.getmembers(cls)
185
if name.startswith(prefix)
188
for name, test in tests:
190
for scenario_name, scenario_params in scenarios:
191
_test = _make_test(name, test, scenario_name, scenario_params)
192
setattr(cls, _test.__name__, _test)
194
return _populate_scenarios
197
def _make_test(name, test, scenario_name, scenario_params):
198
"""Create a test that wraps the given test under the given scenario.
200
:param str name: the name of the test
201
:param callable test: the test method
202
:param str scenario_name: the scenario
203
:param dict scenario_params: the scenario's parameters
208
def _test(self, *args, **kwargs):
209
for k, v in scenario_params.iteritems():
211
return test(self, *args, **kwargs)
213
_test.__name__ = "%s_%s" % (name, scenario_name)