Skip to content

Commit 7d5941d

Browse files
author
Sylvain MARIE
committed
Completed doc and API reference for filters
1 parent a97c743 commit 7d5941d

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

docs/api_reference.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,61 @@ Returns True if the provided object is a function or callable and, if `check_pre
178178

179179
- `check_prefix`: if this boolean is True (default), the prefix will be checked. If False, any function will lead to a `True` result whatever its name.
180180

181+
### The `filters` submodule
182+
183+
This submodule contains symbols to help you create filters for `@parametrize_with_cases(filter=...)`.
184+
185+
All helper filters in this submodule return an instance of `CaseFilter`, so that you can combine them easily with "and" (`&`) "or" (`|`) and "invert" (`~`) in order to create new custom filters.
186+
187+
#### `has_tag`
188+
189+
```python
190+
def has_tag(tag_name: str)
191+
```
192+
193+
Selects cases that have the tag `tag_name`. See `@case(tags=...)` to add tags to a case.
194+
195+
#### `has_tags`
196+
197+
```python
198+
def has_tags(*tag_names: str)
199+
```
200+
201+
Selects cases that have all tags `tag_names`. See `@case(tags=...)` to add tags to a case.
202+
203+
#### `id_has_prefix`
204+
205+
```python
206+
def id_has_prefix(prefix: str)
207+
```
208+
209+
Selects cases that have a case id prefix `prefix`. Note that this is not the prefix of the whole case function name, but the case id, possibly overridden with `@case(id=)`
210+
211+
#### `id_has_suffix`
212+
213+
```python
214+
def id_has_suffix(suffix: str)
215+
```
216+
217+
Selects cases that have a case id suffix `suffix`. Note that this is not the suffix of the whole case function name, but the case id, possibly overridden with `@case(id=)`
218+
219+
220+
#### `id_match_regex`
221+
222+
```python
223+
def id_match_regex(regex: str)
224+
```
225+
226+
Selects cases that have a case id matching regex pattern `regex`. Note that this is not a match of the whole case function name, but the case id, possibly overridden with `@case(id=)`
227+
228+
#### `CaseFilter`
229+
230+
```python
231+
CaseFilter(filter_function: Callable)
232+
```
233+
234+
`CaseFilter` is the class used by all filters above, and implementing logical operations "and" (`&`) "or" (`|`) and "not" (`~`). You can use it to define a composable filter from any callable receiving a single `case` argument and returning a boolean indicating if the `case` is selected.
235+
181236
## 2 - Cases collection
182237

183238
### `@parametrize_with_cases`

docs/index.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,19 @@ def test_foo(a):
292292
@parametrize_with_cases("data", cases='.',
293293
filter=lambda cf: "success" in cf._pytestcase.id)
294294
def test_good_datasets2(data):
295-
assert sqrt(data) > 0
295+
...
296296
```
297297

298-
- pytest-cases offers you an array of default filter for you to choose from in the `filters` module such as: `has_tag`, `has_prefix`, and etc. You can use logical operations on them like "and" (&) "or" (|) and "invert" (~) in order to create you own custom filters.
298+
- An array of default filters is available in the `filters` module: `has_tag`, `id_has_prefix`, etc. You can use logical operations on them such as "and" (`&`) "or" (`|`) and "not" (`~`) in order to create your own custom filters. See [API reference](./api_reference.md#the-filters-submodule) for details.
299299

300300
```python
301-
from pytest_cases import filters
301+
from pytest_cases import filters as ft
302302

303303

304304
@parametrize_with_cases("data", cases='.',
305-
filter=filters.has_tag("success") & filters.id_has_prefix(
306-
"case_b")
305+
filter=ft.has_tag("success") & ft.id_has_prefix("case_b")
307306
def test_good_datasets3(data):
308-
assert sqrt(data) > 0
307+
...
309308
```
310309

311310

0 commit comments

Comments
 (0)