implement a class-based version of pytest parametrize.
(module, cls)
| 404 | |
| 405 | |
| 406 | def _parametrize_cls(module, cls): |
| 407 | """implement a class-based version of pytest parametrize.""" |
| 408 | |
| 409 | if "_sa_parametrize" not in cls.__dict__: |
| 410 | return [cls] |
| 411 | |
| 412 | _sa_parametrize = cls._sa_parametrize |
| 413 | classes = [] |
| 414 | for full_param_set in itertools.product( |
| 415 | *[params for argname, params in _sa_parametrize] |
| 416 | ): |
| 417 | cls_variables = {} |
| 418 | |
| 419 | for argname, param in zip( |
| 420 | [_sa_param[0] for _sa_param in _sa_parametrize], full_param_set |
| 421 | ): |
| 422 | if not argname: |
| 423 | raise TypeError("need argnames for class-based combinations") |
| 424 | argname_split = re.split(r",\s*", argname) |
| 425 | for arg, val in zip(argname_split, param.values): |
| 426 | cls_variables[arg] = val |
| 427 | parametrized_name = "_".join( |
| 428 | re.sub(r"\W", "", token) |
| 429 | for param in full_param_set |
| 430 | for token in param.id.split("-") |
| 431 | ) |
| 432 | name = "%s_%s" % (cls.__name__, parametrized_name) |
| 433 | newcls = type.__new__(type, name, (cls,), cls_variables) |
| 434 | setattr(module, name, newcls) |
| 435 | classes.append(newcls) |
| 436 | return classes |
| 437 | |
| 438 | |
| 439 | _current_class = None |
no test coverage detected