Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Python tools for MODFLOW development and testing.
- [Test models](#test-models)
- [Example scenarios](#example-scenarios)
- [Reusable test case framework](#reusable-test-case-framework)
- [Parametrizing with `Case`](#parametrizing-with-case)
- [Generating cases dynamically](#generating-cases-dynamically)
- [Executables container](#executables-container)
- [Conditionally skipping tests](#conditionally-skipping-tests)
- [Miscellaneous](#miscellaneous)
Expand Down Expand Up @@ -155,22 +157,57 @@ This package provides a minimal framework for self-describing test cases which c

A `Case` requires only a `name`, and has a single default attribute, `xfail=False`, indicating whether the test case is expected to succeed. (Test functions may of course choose to use or ignore this.)

For instance, to generate a set of similar test cases with `pytest-cases`:
#### Parametrizing with `Case`

```python
from pytest_cases import parametrize
`Case` can be used with `@pytest.mark.parametrize()` as usual. For instance:

```python
import pytest
from modflow_devtools.case import Case

template = Case(name="QA")
cases = [
template.copy_update(name=template.name + "1", question="What's the meaning of life, the universe, and everything?", answer=42),
template.copy_update(name=template.name + "2", question="Is a Case immutable?", answer="No, but it's better not to mutate it.")
template.copy_update(name=template.name + "1",
question="What's the meaning of life, the universe, and everything?",
answer=42),
template.copy_update(name=template.name + "2",
question="Is a Case immutable?",
answer="No, but it's probably best not to mutate it.")
]

@parametrize(data=cases, ids=[c.name for c in cases])
def case_qa(case):
print(case.name, case.question, case.answer)

@pytest.mark.parametrize("case", cases)
def test_cases(case):
assert len(cases) == 2
assert cases[0] != cases[1]
```

#### Generating cases dynamically

One pattern possible with `pytest-cases` is to programmatically generate test cases by parametrizing a function. This can be a convenient way to produce several similar test cases from a template:

```python
from pytest_cases import parametrize, parametrize_with_cases
from modflow_devtools.case import Case


template = Case(name="QA")
gen_cases = [template.copy_update(name=f"{template.name}{i}", question=f"Q{i}", answer=f"A{i}") for i in range(3)]
info = "cases can be modified further in the generator function,"\
" or the function may construct and return another object"


@parametrize(case=gen_cases, ids=[c.name for c in gen_cases])
def qa_cases(case):
return case.copy_update(info=info)


@parametrize_with_cases("case", cases=".", prefix="qa_")
def test_qa(case):
assert "QA" in case.name
assert info == case.info
print(f"{case.name}:", f"{case.question}? {case.answer}")
print(case.info)
```

### Executables container
Expand Down
10 changes: 7 additions & 3 deletions modflow_devtools/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class Case(SimpleNamespace):
Minimal container for a reusable test case.
"""

def __init__(self, **kwargs):
def __init__(self, case: "Case" = None, **kwargs):
if case is not None:
super().__init__(**case.__dict__.copy())
return

if "name" not in kwargs:
raise ValueError(f"Case name is required")

Expand All @@ -26,7 +30,7 @@ def copy(self):
Copies the test case.
"""

return SimpleNamespace(**self.__dict__.copy())
return Case(**self.__dict__.copy())

def copy_update(self, **kwargs):
"""
Expand All @@ -36,4 +40,4 @@ def copy_update(self, **kwargs):

cpy = self.__dict__.copy()
cpy.update(kwargs)
return SimpleNamespace(**cpy)
return Case(**cpy)
Loading