-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Case): add class for reusable test case data
- Loading branch information
Showing
4 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from types import SimpleNamespace | ||
|
||
|
||
class Case(SimpleNamespace): | ||
""" | ||
Minimal container for a reusable test case. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
if "name" not in kwargs: | ||
raise ValueError(f"Case name is required") | ||
|
||
# set defaults | ||
if "xfail" not in kwargs: | ||
kwargs["xfail"] = False | ||
# if 'compare' not in kwargs: | ||
# kwargs['compare'] = True | ||
|
||
super().__init__(**kwargs) | ||
|
||
def __repr__(self): | ||
return self.name | ||
|
||
def copy(self): | ||
""" | ||
Copies the test case. | ||
""" | ||
|
||
return SimpleNamespace(**self.__dict__.copy()) | ||
|
||
def copy_update(self, **kwargs): | ||
""" | ||
A utility method for copying a test case with changes. | ||
Recommended for dynamically generating similar cases. | ||
""" | ||
|
||
cpy = self.__dict__.copy() | ||
cpy.update(kwargs) | ||
return SimpleNamespace(**cpy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pytest | ||
from modflow_devtools.case import Case | ||
|
||
|
||
def test_requires_name(): | ||
with pytest.raises(ValueError): | ||
Case() | ||
|
||
|
||
def test_defaults(): | ||
assert not Case(name="test").xfail | ||
|
||
|
||
def test_copy(): | ||
case = Case(name="test", foo="bar") | ||
copy = case.copy() | ||
|
||
assert case is not copy | ||
assert case == copy | ||
|
||
|
||
def test_copy_update(): | ||
case = Case(name="test", foo="bar") | ||
copy = case.copy_update() | ||
|
||
assert case is not copy | ||
assert case == copy | ||
|
||
copy2 = case.copy_update(foo="baz") | ||
|
||
assert copy is not copy2 | ||
assert copy.foo == "bar" | ||
assert copy2.foo == "baz" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ test = | |
%(lint)s | ||
coverage | ||
flaky | ||
pytest-cases | ||
pytest-cov | ||
pytest-dotenv | ||
pytest-xdist | ||
|