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

Add: get_all_cases extended to support filtering and use other modules as parametrization_target #260

Merged
merged 5 commits into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 14 additions & 3 deletions docs/api_reference.md
Expand Up @@ -280,7 +280,7 @@ Note that `@parametrize_with_cases` collection and parameter creation steps are

```python
# Collect all cases
cases_funs = get_all_cases(f, cases=cases, prefix=prefix,
cases_funs = get_all_cases(f, cases=cases, prefix=prefix,
glob=glob, has_tag=has_tag, filter=filter)

# Transform the various functions found
Expand Down Expand Up @@ -335,16 +335,27 @@ Note that you can get the same contents directly by using the [`current_cases`](
### `get_all_cases`

```python
def get_all_cases(parametrization_target: Callable,
def get_all_cases(parametrization_target: Callable = None,
cases: Union[Callable, Type, ModuleRef] = None,
prefix: str = 'case_',
glob: str = None,
has_tag: Union[str, Iterable[str]] = None,
filter: Callable[[Callable], bool] = None
) -> List[Callable]:
```
Collect all cases as used with [`@parametrize_with_cases`](#parametrize_with_cases). See [`@parametrize_with_cases`](#parametrize_with_cases) for details on the parameters.

Lists all desired cases for a given `parametrization_target` (a test function or a fixture). This function may be convenient for debugging purposes. See [`@parametrize_with_cases`](#parametrize_with_cases) for details on the parameters.
This can be used to lists all desired cases for a given `parametrization_target` (a test function or a fixture) which may be convenient for debugging purposes.

- If `cases` is `AUTO` or contains a string module reference, `parametrization_target` must be provided.

```python
# Without a parametrization target
cases = get_all_cases(cases=[case_1, case_2, case_3], has_tag=["banana"])

# With a parametrization target
cases = get_all_cases(f, cases=".", has_tag=["banana"])
```


### `get_parametrize_args`
Expand Down
23 changes: 14 additions & 9 deletions src/pytest_cases/case_parametrizer_new.py
Expand Up @@ -37,7 +37,7 @@

from .fixture_core1_unions import USED, NOT_USED
from .fixture_core2 import CombinedFixtureParamValue, fixture
from .fixture__creation import check_name_available, CHANGE
from .fixture__creation import check_name_available, get_caller_module, CHANGE
from .fixture_parametrize_plus import fixture_ref, _parametrize_plus, FixtureParamAlternative, ParamAlternative, \
SingleParamAlternative, MultiParamAlternative, FixtureRefItem

Expand Down Expand Up @@ -205,19 +205,20 @@ def _glob_name_filter(case_fun):
return _glob_name_filter


def get_all_cases(parametrization_target, # type: Callable
cases=None, # type: Union[Callable, Type, ModuleRef]
prefix=CASE_PREFIX_FUN, # type: str
glob=None, # type: str
has_tag=None, # type: Union[str, Iterable[str]]
filter=None # type: Callable[[Callable], bool] # noqa
def get_all_cases(parametrization_target=None, # type: Callable
cases=None, # type: Union[Callable, Type, ModuleRef]
prefix=CASE_PREFIX_FUN, # type: str
glob=None, # type: str
has_tag=None, # type: Union[str, Iterable[str]]
filter=None # type: Callable[[Callable], bool] # noqa
):
# type: (...) -> List[Callable]
"""
Lists all desired cases for a given `parametrization_target` (a test function or a fixture). This function may be
convenient for debugging purposes. See `@parametrize_with_cases` for details on the parameters.

:param parametrization_target: a test function
:param parametrization_target: a test function to get the module reference from. Required for cases that
smarie marked this conversation as resolved.
Show resolved Hide resolved
rely on module reference.
:param cases: a case function, a class containing cases, a module or a module name string (relative module
names accepted). Or a list of such items. You may use `THIS_MODULE` or `'.'` to include current module.
`AUTO` (default) means that the module named `test_<name>_cases.py` will be loaded, where `test_<name>.py` is
Expand Down Expand Up @@ -266,7 +267,11 @@ def get_all_cases(parametrization_target, # type: Callable
filters += (filter,)

# parent package
caller_module_name = getattr(parametrization_target, '__module__', None)
if parametrization_target is None:
caller_module_name = getattr(parametrization_target, '__module__', None)
else:
caller_module_name = get_caller_module()
eddiebergman marked this conversation as resolved.
Show resolved Hide resolved

parent_pkg_name = '.'.join(caller_module_name.split('.')[:-1]) if caller_module_name is not None else None

# start collecting all cases
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions tests/cases/issues/issue_258/cases.py
@@ -0,0 +1,10 @@
from pytest_cases import case

@case
def case_1():
return "hello world"


@case
def case_2():
return "hello mars"
11 changes: 11 additions & 0 deletions tests/cases/issues/issue_258/cases_issue_258.py
@@ -0,0 +1,11 @@
from pytest_cases import case


@case
def case_1():
return "hello world"


@case
def case_2():
return "hello mars"
79 changes: 79 additions & 0 deletions tests/cases/issues/issue_258/test_issue_258.py
@@ -0,0 +1,79 @@
from pytest_cases import get_all_cases, case, parametrize_with_cases, parametrize
import pytest
from pytest_cases.case_parametrizer_new import AUTO


# Test behaviour without a string module ref
############################################

@case(tags=["a", "banana"])
def case_1():
return "a_banana"


@case(tags=["a"])
def case_2():
return "a"


@case(tags=["b", "banana"])
def case_3():
return "b_banana"


@case(tags=["b"])
def case_4():
return "b"


all_cases = get_all_cases(cases=[case_1, case_2, case_3, case_4])

a_cases = get_all_cases(cases=all_cases, has_tag="a")
b_cases = get_all_cases(cases=all_cases, has_tag="b")

banana_cases = get_all_cases(cases=a_cases + b_cases, has_tag=["banana"])


@parametrize_with_cases("word", cases=all_cases)
def test_all(word):
assert word in ["a", "a_banana", "b", "b_banana"]


@parametrize_with_cases("word", cases=a_cases)
def test_a(word):
assert "a" in word


@parametrize_with_cases("word", cases=b_cases)
def test_b(word):
assert "b" in word


@parametrize_with_cases("word", cases=banana_cases)
def test_banana(word):
assert "banana" in word


def test_get_cases_without_parametrization_target():
assert len(list(all_cases)) == 4
assert len(list(a_cases)) == 2
assert len(list(b_cases)) == 2
assert len(list(banana_cases)) == 2


@parametrize("ref", ['.'])
def test_get_all_cases_raises_with_module_case(ref):
with pytest.raises(ValueError, match="Cases beginning with"):
get_all_cases(cases=ref)


# Test behaviour with string module ref
#######################################
def test_relative_import_cases_is_none_empty():
relative_import_cases = get_all_cases(cases=".cases")
assert len(relative_import_cases) == 2


def test_auto_import_cases_is_non_empty():
auto_import_cases = get_all_cases(cases=AUTO)
assert len(auto_import_cases) == 2