@@ -37,15 +37,16 @@ async def test_invalid_response(create_redis):
3737 fake_stream = MockStream (raw + b"\r \n " )
3838
3939 parser : _AsyncRESPBase = r .connection ._parser
40- with mock .patch .object (parser , "_stream" , fake_stream ):
41- with pytest .raises (InvalidResponse ) as cm :
42- await parser .read_response ()
40+
4341 if isinstance (parser , _AsyncRESPBase ):
44- assert str ( cm . value ) = = f"Protocol Error: { raw !r} "
42+ exp_err = f"Protocol Error: { raw !r} "
4543 else :
46- assert (
47- str (cm .value ) == f'Protocol error, got "{ raw .decode ()} " as reply type byte'
48- )
44+ exp_err = f'Protocol error, got "{ raw .decode ()} " as reply type byte'
45+
46+ with mock .patch .object (parser , "_stream" , fake_stream ):
47+ with pytest .raises (InvalidResponse , match = exp_err ):
48+ await parser .read_response ()
49+
4950 await r .connection .disconnect ()
5051
5152
@@ -171,10 +172,9 @@ async def test_connect_timeout_error_without_retry():
171172 conn ._connect = mock .AsyncMock ()
172173 conn ._connect .side_effect = socket .timeout
173174
174- with pytest .raises (TimeoutError ) as e :
175+ with pytest .raises (TimeoutError , match = "Timeout connecting to server" ) :
175176 await conn .connect ()
176177 assert conn ._connect .call_count == 1
177- assert str (e .value ) == "Timeout connecting to server"
178178
179179
180180@pytest .mark .onlynoncluster
@@ -532,17 +532,14 @@ async def test_format_error_message(conn, error, expected_message):
532532
533533
534534async def test_network_connection_failure ():
535- exp_err = fr "^Error { ECONNREFUSED } connecting to 127.0.0.1:9999.(.+)$"
535+ exp_err = rf "^Error { ECONNREFUSED } connecting to 127.0.0.1:9999.(.+)$"
536536 with pytest .raises (ConnectionError , match = exp_err ):
537537 redis = Redis (host = "127.0.0.1" , port = 9999 )
538538 await redis .set ("a" , "b" )
539539
540540
541541async def test_unix_socket_connection_failure ():
542- with pytest .raises (ConnectionError ) as e :
542+ exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
543+ with pytest .raises (ConnectionError , match = exp_err ):
543544 redis = Redis (unix_socket_path = "unix:///tmp/a.sock" )
544545 await redis .set ("a" , "b" )
545- assert (
546- str (e .value )
547- == "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
548- )
0 commit comments