Skip to content

Commit

Permalink
Support stacked pytest.param.
Browse files Browse the repository at this point in the history
Refs #17.
  • Loading branch information
coady committed Oct 6, 2024
1 parent 6c631e1 commit 9a8c80f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
## Unreleased
### Changed
* Python >=3.9 required
* `pytest.param` supported

## [1.5](https://pypi.org/project/pytest-parametrized/1.5/) - 2023-11-03
### Changed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def test_foo(x=[0, 1], y=[2, 3]):
pass
```

`pytest.param` is supported for single values or `.product`.

## fixtures
[Parametrized fixtures](https://docs.pytest.org/en/latest/how-to/fixtures.html#fixture-parametrize) which simply return their param.

Expand Down
12 changes: 7 additions & 5 deletions parametrized.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ def parametrized(func, combine=None, **kwargs):
argspec = inspect.getfullargspec(func)
params = dict(zip(reversed(argspec.args), reversed(argspec.defaults)))
func.__defaults__ = () # pytest ignores params with defaults
if combine is None:
(args,) = params.items() # multiple keywords require combine function, e.g., zip
else:
args = ','.join(params), combine(*params.values())
return pytest.mark.parametrize(*args, **kwargs)(func)
if combine is None and len(params) > 1:
raise ValueError("multiple keywords require combine function, e.g., zip")
if combine not in (None, itertools.product):
params = {','.join(params): combine(*params.values())}
for param in params.items():
func = pytest.mark.parametrize(*param, **kwargs)(func)
return func


def fixture(*params, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Issues = "https://github.com/coady/pytest-parametrized/issues"
line-length = 100

[tool.ruff.format]
quote-style = "single"
quote-style = "preserve"

[tool.coverage.run]
source = ["parametrized"]
Expand Down
5 changes: 5 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ def test_error(name='abc', value=range(3)):
@parametrized
def test(name=(), value=()):
pass


@parametrized.product
def test_param(key=[0], value=[0, pytest.param(1, marks=pytest.mark.xfail())]):
assert key == value

0 comments on commit 9a8c80f

Please sign in to comment.