-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pepkit/dev
0.0.4
- Loading branch information
Showing
11 changed files
with
241 additions
and
19 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
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 @@ | ||
pytest>=3.0.7 |
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
pytest>=3.0.7 | ||
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,70 @@ | ||
""" Tests for ExpectContext """ | ||
|
||
import pytest | ||
from ubiquerg import ExpectContext | ||
|
||
__author__ = "Vince Reuter" | ||
__email__ = "vreuter@virginia.edu" | ||
|
||
|
||
def atterr(*args, **kwargs): | ||
raise AttributeError() | ||
|
||
|
||
def ioerr(*args, **kwargs): | ||
raise IOError() | ||
|
||
|
||
def keyerr(*args, **kwargs): | ||
raise KeyError() | ||
|
||
|
||
def typerr(*args, **kwargs): | ||
raise TypeError() | ||
|
||
|
||
FUN_BY_ERR = {IOError: ioerr, TypeError: typerr, | ||
KeyError: keyerr, AttributeError: atterr} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["err", "fun", "expect_success"], | ||
[t for err, fun in FUN_BY_ERR.items() | ||
for t in [(err, fun, True)] + | ||
[(err, unfun, False) for unfun in | ||
[f for e, f in FUN_BY_ERR.items() if e != err]]]) | ||
@pytest.mark.parametrize("args", [tuple(), ("a", ), ("b", 1)]) | ||
@pytest.mark.parametrize("kwargs", [{}, {"a": 0}, {"a": 1, "b": [1, 2]}]) | ||
def test_exp_ctx_exceptional_result(err, fun, expect_success, args, kwargs): | ||
""" The expectation context correctly handles exceptional expectation. """ | ||
try: | ||
with ExpectContext(expected=err, test_func=fun) as ctx: | ||
ctx(*args, **kwargs) | ||
except AssertionError as e: | ||
if expect_success: | ||
# Wrong failure type | ||
pytest.fail(str(e)) | ||
except Exception as e: | ||
if expect_success: | ||
pytest.fail("Expected to catch a {} but hit {}". | ||
format(type(err), type(e))) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["exp_res", "fun", "expect_success"], | ||
[(2, lambda *args, **kwargs: 2, True), | ||
(-1, lambda *args, **kwargs: 0, False)]) | ||
@pytest.mark.parametrize("args", [tuple(), ("a", ), ("b", 1)]) | ||
@pytest.mark.parametrize("kwargs", [{}, {"a": 0}, {"a": 1, "b": [1, 2]}]) | ||
def test_exp_ctx_ordinary_result(exp_res, fun, expect_success, args, kwargs): | ||
""" The expectation context correctly handles ordinary expectation. """ | ||
try: | ||
with ExpectContext(expected=exp_res, test_func=fun) as ctx: | ||
res = ctx(*args, **kwargs) | ||
except AssertionError as e: | ||
if expect_success: | ||
pytest.fail("Expected success but assertion failed: {}".format(e)) | ||
else: | ||
if not expect_success: | ||
pytest.fail("Unexpected function execution success (feigned " | ||
"expectation {} and got {})".format(exp_res, res)) |
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
""" Package exports """ | ||
|
||
from .assistest import * | ||
from .cli_tools import * | ||
from .collection import * | ||
from ._version import __version__ |
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.3" | ||
__version__ = "0.0.4dev" |
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,35 @@ | ||
""" Assistance functions for writing and running tests """ | ||
|
||
import pytest | ||
|
||
__author__ = "Vince Reuter" | ||
__email__ = "vreuter@virginia.edu" | ||
|
||
|
||
class ExpectContext(object): | ||
""" Pytest validation context, a framework for varied kinds of expectations. """ | ||
|
||
def __init__(self, expected, test_func): | ||
""" | ||
Create the test context by specifying expectation and function. | ||
:param object | type expected: expected result or exception | ||
:param callable test_func: the callable object to test | ||
""" | ||
self._f = test_func | ||
self._exp = expected | ||
|
||
def __enter__(self): | ||
""" Return the instance for use as a callable. """ | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
|
||
def __call__(self, *args, **kwargs): | ||
""" Execute the instance's function, passing given args/kwargs. """ | ||
if isinstance(self._exp, type) and issubclass(self._exp, Exception): | ||
with pytest.raises(self._exp): | ||
self._f(*args, **kwargs) | ||
else: | ||
assert self._exp == self._f(*args, **kwargs) |
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