Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support removal from fixture closure tree to improve compatibility with other plugins #272

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 3.6.12 - type hint fix

- Fixed type hint errors detected by `pyright`. Fixed [#270](https://github.com/smarie/python-pytest-cases/issues/270)

### 3.6.11 - bugfix for pytest-xdist and `get_all_cases` API improvement

- `get_all_cases` can now be called without `parametrization_target` (defaulting to the caller module), and with an explicit module object. Fixed [#258](https://github.com/smarie/python-pytest-cases/issues/258). PR [#260](https://github.com/smarie/python-pytest-cases/pull/260) by [@eddiebergman](https://github.com/eddiebergman).
Expand Down
8 changes: 7 additions & 1 deletion src/pytest_cases/fixture_parametrize_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
from collections import Iterable

try:
from typing import Union, Callable, List, Any, Sequence, Optional, Type, Tuple # noqa
from typing import Union, Callable, List, Any, Sequence, Optional, Type, Tuple, TypeVar # noqa
from types import ModuleType # noqa

T = TypeVar('T', bound=Union[Type, Callable])
except ImportError:
pass

Expand Down Expand Up @@ -624,6 +626,7 @@ def parametrize(argnames=None, # type: Union[str, Tuple[str], List[str]]
hook=None, # type: Callable[[Callable], Callable]
debug=False, # type: bool
**args):
# type: (...) -> Callable[[T], T]
"""
Equivalent to `@pytest.mark.parametrize` but also supports

Expand Down Expand Up @@ -733,6 +736,7 @@ def _parametrize_plus(argnames=None, # type: Union[str, Tuple[str], List[str]]
hook=None, # type: Callable[[Callable], Callable]
debug=False, # type: bool
**args):
# type: (...) -> Tuple[Callable[[T], T], bool]
"""

:return: a tuple (decorator, needs_inject) where needs_inject is True if decorator has signature (f, host)
Expand Down Expand Up @@ -790,6 +794,7 @@ def _make_ids(**args):
else:
# wrap the decorator to check if the test function has the parameters as arguments
def _apply(test_func):
# type: (...) -> Callable[[T], T]
if not safe_isclass(test_func):
# a Function: raise a proper error message if improper use
s = signature(test_func)
Expand Down Expand Up @@ -923,6 +928,7 @@ def _create_fixture_ref_product(fh, union_name, i, fixture_ref_positions, test_f

# Then create the decorator per se
def parametrize_plus_decorate(test_func, fixtures_dest):
# type: (...) -> Callable[[T], T]
"""
A decorator that wraps the test function so that instead of receiving the parameter names, it receives the
new fixture. All other decorations are unchanged.
Expand Down
45 changes: 45 additions & 0 deletions tests/pytest_extension/issues/test_issue_269.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest
from pytest_cases import fixture, parametrize


@pytest.fixture
def __my_repeat_step_number(request):
return request.param


@pytest.hookimpl(trylast=True)
def pytest_generate_tests(metafunc):
"""This hook and fixture above are similar to what happens in pytest-repeat.
See https://github.com/smarie/python-pytest-cases/issues/269
"""
metafunc.fixturenames.append("__my_repeat_step_number")

def make_progress_id(i, n=2):
return '{0}-{1}'.format(i + 1, n)

scope = metafunc.config.option.repeat_scope
metafunc.parametrize(
'__my_repeat_step_number',
range(2),
indirect=True,
ids=make_progress_id,
scope=scope
)


@fixture
def my_fix():
return 2


@parametrize("arg", (my_fix,))
def test_repeat(arg):
assert arg == 2


def test_synthesis(module_results_dct):
"""Make sure that two tests were created."""
assert list(module_results_dct) == [
"test_repeat-1-2",
"test_repeat-2-2"
]