Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
pytest doesn't fail from pytest.raises with wrong error
Browse files Browse the repository at this point in the history
  • Loading branch information
popravich committed Jan 16, 2017
1 parent 914f3eb commit b7eff90
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,21 @@ def raises_regex(exc_type, message):
.format(message, str(exc_info.value)))


@contextlib.contextmanager
def raises_only(exc_type, message=None):
try:
with pytest.raises(exc_type) as exc_info:
yield exc_info
except Exception as unexpected:
pytest.fail("Raised {!r} instead of {!r}".format(unexpected, exc_type))
else:
if message is not None:
match = re.search(message, str(exc_info.value))
assert match is not None, (
"Pattern {!r} does not match {!r}"
.format(message, str(exc_info.value)))


def logs(logger, level=None):
"""Catches logs for given logger and level.
Expand Down Expand Up @@ -649,6 +664,7 @@ def assert_almost_equal(first, second, places=None, msg=None, delta=None):
def pytest_namespace():
return {
'raises_regex': raises_regex,
'raises_only': raises_only,
'assert_almost_equal': assert_almost_equal,
'redis_version': redis_version,
'logs': logs,
Expand Down
5 changes: 3 additions & 2 deletions tests/connection_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ def test_ping(redis):


@pytest.mark.run_loop
def test_quit(redis):
def test_quit(redis, loop):
try:
assert b'OK' == (yield from redis.quit())
except asyncio.CancelledError:
pass
yield from asyncio.sleep(0, loop)

if not isinstance(redis._pool_or_conn, ConnectionsPool):
with pytest.raises(ConnectionClosedError):
with pytest.raises_only(ConnectionClosedError):
yield from redis.ping()


Expand Down

0 comments on commit b7eff90

Please sign in to comment.