Pytest failed with - ConnectionRefusedError: [Errno 61] Connection refused


  • pytest ==5.3.5
  • sanic ==19.12.2
  • python 3.7.6
  • macos 10.15.3

I have created a dummy sanic project with a simple pytest file

from sanic import Sanic
from sanic.response import empty

app = Sanic(__name__)

async def dummy_get(request):
    return empty()

def test_dummy_get():
    _, response = app.test_client.get('/')
    assert response.status == 204

Running pytest gives me:

[2020-02-24 21:47:22 +0800] [81113] [INFO] Goin' Fast @
[2020-02-24 21:47:22 +0800] [81113] [INFO]
[2020-02-24 21:47:22 +0800] [81113] [ERROR] Exception
Traceback (most recent call last):
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/sanic/", line 120, in _collect_response
    method, url, *request_args, **request_kwargs
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/sanic/", line 41, in _local_request
    url, verify=False, *args, **kwargs
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/", line 671, in get
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/", line 268, in request
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/", line 410, in send
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/", line 470, in send_handling_redirects
    request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/", line 599, in send_single_request
    request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/", line 204, in send
    request=request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/", line 126, in send
    raise exc
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/", line 121, in send
    request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/", line 53, in send
    await self.connect(verify=verify, cert=cert, timeout=timeout)
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/", line 82, in connect
    host, port, ssl_context, timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/concurrency/", line 33, in open_tcp_stream
    return await self.backend.open_tcp_stream(hostname, port, ssl_context, timeout)
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/concurrency/", line 267, in open_tcp_stream
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/", line 442, in wait_for
    return fut.result()
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/", line 77, in open_connection
    lambda: protocol, host, port, **kwds)
  File "uvloop/loop.pyx", line 1974, in create_connection
  File "uvloop/loop.pyx", line 1951, in uvloop.loop.Loop.create_connection
ConnectionRefusedError: [Errno 61] Connection refused
[2020-02-24 21:47:22 +0800] [81113] [INFO] Starting worker [81113]
[2020-02-24 21:47:22 +0800] [81113] [INFO] Stopping worker [81113]
[2020-02-24 21:47:22 +0800] [81113] [INFO] Server Stopped

I have also tried specifying port=None and port=8080 for SanicTestClient but got the same error.

When I run the server directly, everything works as normal.

Any ideas?


After restarting my Macbook Pro, test started passing.

This is interesting. If it starts happening again, please open an issue. I’m concerned that this is a deeper problem.

After more investigation, I found out what was going on.

My Macbook Pro uses a SOCKS proxy to connect to the internet (being a developer behind the GFW really sucks). The proxy setting (all_proxy= to be more specific) gets propagated to the PyCharm run env, which inherits system env by default. I have recently updated the proxy port but apparently PyCharm did not pick up the updated port, hence causing Connection refused error.

Updating the run env by removing all_proxy= will resolve this particular problem. But only when I turn off my proxy completely do all the tests pass eventually.

This makes me think that running tests behind a proxy will cause some unexpected issues in general.

Thank you for the update, @davidygs - I appreciate you running it down for us!