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 9c950fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions aioredis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def _read_data(self):
break
if data == b'' and self._reader.at_eof():
logger.debug("Connection has been closed by server")
break
self._parser.feed(data)
while True:
try:
Expand Down
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
7 changes: 4 additions & 3 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

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


Expand Down

0 comments on commit 9c950fd

Please sign in to comment.