|
7 | 7 | [](https://smarie.github.io/python-pytest-cases/) [](https://pypi.python.org/pypi/pytest-cases/) [](https://pepy.tech/project/pytest-cases) [](https://pepy.tech/project/pytest-cases) [](https://github.com/smarie/python-pytest-cases/stargazers) |
8 | 8 | [](https://doi.org/10.5281/zenodo.3937830) |
9 | 9 |
|
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." |
11 | 11 |
|
12 | 12 | !!! success "`@parametrize` now automatically detects fixture symbols ! See [documentation](./pytest_goodies.md#parametrize) for details." |
13 | 13 |
|
@@ -286,11 +286,13 @@ def test_foo(a): |
286 | 286 | assert a > 0 |
287 | 287 | ``` |
288 | 288 |
|
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). |
290 | 290 |
|
291 | 291 | ```python |
| 292 | +from pytest_cases import get_case_id |
| 293 | + |
292 | 294 | @parametrize_with_cases("data", cases='.', |
293 | | - filter=lambda cf: "success" in cf._pytestcase.id) |
| 295 | + filter=lambda cf: "success" in get_case_id(cf)) |
294 | 296 | def test_good_datasets2(data): |
295 | 297 | ... |
296 | 298 | ``` |
@@ -467,7 +469,24 @@ def test_caching(cached_a, d): |
467 | 469 |
|
468 | 470 | !!! warning "If you add a cache mechanism, make sure that your test functions do not modify the returned objects !" |
469 | 471 |
|
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 |
471 | 490 |
|
472 | 491 | 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. |
473 | 492 |
|
@@ -513,7 +532,7 @@ test_doc_ids_debug.py::test_foo[#hello_name#-earthling] |
513 | 532 | ============================== 4 passed in 0.07s ============================== |
514 | 533 | ``` |
515 | 534 |
|
516 | | -### d- Debugging |
| 535 | +### e- Debugging |
517 | 536 |
|
518 | 537 | 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`. |
519 | 538 |
|
|
0 commit comments