Skip to content

Commit a3aa669

Browse files
author
Sylvain MARIE
committed
Added doc about get_current_case_id
1 parent b65734b commit a3aa669

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

docs/api_reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ argvalues = get_parametrize_args(host_class_or_module_of_f, cases_funs)
288288
- `scope`: The scope of the union fixture to create if `fixture_ref`s are found in the argvalues
289289

290290

291+
### `get_current_case_id`
292+
293+
```python
294+
def get_current_case_id(request_or_item,
295+
argnames: str
296+
):
297+
```
298+
299+
A helper function to return the current case id for a given `pytest` item (available in some hooks) or `request`
300+
(available in hooks, and also directly as a fixture).
301+
302+
You need to provide the argname(s) used in the corresponding `@parametrize_with_cases` so that this method finds
303+
the right id.
304+
291305
### `get_all_cases`
292306

293307
```python

docs/index.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Documentation](https://img.shields.io/badge/doc-latest-blue.svg)](https://smarie.github.io/python-pytest-cases/) [![PyPI](https://img.shields.io/pypi/v/pytest-cases.svg)](https://pypi.python.org/pypi/pytest-cases/) [![Downloads](https://pepy.tech/badge/pytest-cases)](https://pepy.tech/project/pytest-cases) [![Downloads per week](https://pepy.tech/badge/pytest-cases/week)](https://pepy.tech/project/pytest-cases) [![GitHub stars](https://img.shields.io/github/stars/smarie/python-pytest-cases.svg)](https://github.com/smarie/python-pytest-cases/stargazers)
88
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3937830.svg)](https://doi.org/10.5281/zenodo.3937830)
99

10-
!!! success "Major refactoring of test ids in v3.0.0 ! See [below](#c-test-ids) for details."
10+
!!! success "Major refactoring of test ids in v3.0.0 ! See [below](#d-test-ids) for details."
1111

1212
!!! success "`@parametrize` now automatically detects fixture symbols ! See [documentation](./pytest_goodies.md#parametrize) for details."
1313

@@ -286,11 +286,13 @@ def test_foo(a):
286286
assert a > 0
287287
```
288288

289-
- Finally if none of the above matches your expectations, you can provide a callable to `filter`. This callable will receive each collected case function and should return `True` (or a truth-value convertible object) in case of success. Note that your function can leverage the `_pytestcase` attribute available on the case function to read the tags, marks and id found on it.
289+
- Finally if none of the above matches your expectations, you can provide a callable to `filter`. This callable will receive each collected case function and should return `True` (or a truth-value convertible object) in case of success. Note that your function can leverage the `get_case_id`, `get_case_marks`, `get_case_tags` etc. helper functions to read the tags, marks and id found on it. See [API doc](./api_reference.md#get_case_id).
290290

291291
```python
292+
from pytest_cases import get_case_id
293+
292294
@parametrize_with_cases("data", cases='.',
293-
filter=lambda cf: "success" in cf._pytestcase.id)
295+
filter=lambda cf: "success" in get_case_id(cf))
294296
def test_good_datasets2(data):
295297
...
296298
```
@@ -467,7 +469,24 @@ def test_caching(cached_a, d):
467469
468470
!!! warning "If you add a cache mechanism, make sure that your test functions do not modify the returned objects !"
469471
470-
### c- Test ids
472+
### c- Accessing the current case id
473+
474+
You may need to access the current case id from within a `pytest` hook or the test itself. For this the `get_current_case_id` helper function is provided:
475+
476+
```python
477+
from pytest_cases import parametrize_with_cases, get_current_case_id
478+
479+
def case_a():
480+
return 1
481+
482+
@parametrize_with_cases("data", cases=case_a)
483+
def test_lazy_val_case(data, request):
484+
assert get_current_case_id(request, "data") == "a"
485+
```
486+
487+
See [API reference](./api_reference.md#get_current_case_id) for details.
488+
489+
### d- Test ids
471490
472491
Starting from version 3.0.0, test ids induced by `@parametrize_with_cases` are similar to the ids induced by `@pytest.mark.parametrize`, even if a case function is itself parametrized or requires a fixture. In some situations you may wish to get a better control on the test ids.
473492
@@ -513,7 +532,7 @@ test_doc_ids_debug.py::test_foo[#hello_name#-earthling]
513532
============================== 4 passed in 0.07s ==============================
514533
```
515534
516-
### d- Debugging
535+
### e- Debugging
517536
518537
When all of your case functions are simple, `@parametrize_with_cases` generates a `@parametrize` decorator with argvalues being a list of `lazy_value(<case_func>)` for all of them. This in turn falls back to a good old `@pytest.mark.parametrize`, so the behaviour is close to what you are used to see when using `pytest`.
519538

0 commit comments

Comments
 (0)