requests icon indicating copy to clipboard operation
requests copied to clipboard

test_stream_timeout and test_urllib3_pool_connection_closed pass without asserting when no exception is raised

Open jeffev opened this issue 1 month ago • 2 comments

Two tests in tests/test_requests.py put their only assertion inside an except block, so they pass without checking anything when the exception is not raised.

TestTimeout.test_stream_timeout: if the request to delay/10 completed without timing out, the test would pass. Switching it to pytest.raises (like test_invalid_timeout right below it) makes it fail in that case:

def test_stream_timeout(self, httpbin):
    with pytest.raises(requests.exceptions.Timeout) as e:
        requests.get(httpbin("delay/10"), timeout=2.0)
    assert "Read timed out" in e.value.args[0].args[0]

I checked that this version passes as is and fails with DID NOT RAISE when pointed at delay/0. The change is in this commit on my fork, if it's useful: https://github.com/jeffev/requests/commit/b5058def5811

test_urllib3_pool_connection_closed: with the current urllib3, s.get() on an adapter mounted with pool_connections=0, pool_maxsize=0 succeeds (I get a 200), so the except ConnectionError branch never runs and the test checks nothing. I'm not sure what behavior it should pin down now, so I didn't touch it.

I'd have opened a PR, but pull requests seem limited to collaborators.

jeffev avatar Nov 01 '26 00:11 jeffev

Both tests have the same defect, and it is the kind that hides a regression rather than causing a failure: the assertion lives inside the except block, so if the call does not raise, the test body completes and pytest reports a pass.

tests/test_requests.py:2537

def test_stream_timeout(self, httpbin):
    try:
        requests.get(httpbin("delay/10"), timeout=2.0)
    except requests.exceptions.Timeout as e:
        assert "Read timed out" in e.args[0].args[0]

tests/test_requests.py:2750

def test_urllib3_pool_connection_closed(httpbin):
    ...
    try:
        s.get(httpbin("status/200"))
    except ConnectionError as e:
        assert "Pool is closed." in str(e)

If the timeout is not raised - which is precisely the regression these tests exist to catch - test_stream_timeout passes. Same for the pool test if the request succeeds.

The fix is pytest.raises, which fails when nothing is raised and keeps the message check:

def test_stream_timeout(self, httpbin):
    with pytest.raises(requests.exceptions.Timeout) as excinfo:
        requests.get(httpbin("delay/10"), timeout=2.0)
    assert "Read timed out" in str(excinfo.value)

For the pool test, pytest.raises(ConnectionError, match="Pool is closed") is enough. Worth also keeping the nested e.args[0].args[0] version somewhere if the point is to pin the wrapped message, but str(excinfo.value) reads better and does not depend on the exception's internal structure.

A quick grep for except .* as e: followed by an assert in the same block would probably find a couple more in this file.

Esmail-sarhadi avatar Nov 02 '26 09:11 Esmail-sarhadi

Here is a complete solution for both tests, including test_urllib3_pool_connection_closed:

1. test_stream_timeout

Wrapping in pytest.raises ensures the test fails if no timeout exception is raised:

    def test_stream_timeout(self, httpbin):
        with pytest.raises(requests.exceptions.Timeout) as excinfo:
            requests.get(httpbin("delay/10"), timeout=2.0)
        assert "Read timed out" in str(excinfo.value)

2. test_urllib3_pool_connection_closed

The reason this test was passing silently is that mounting an adapter with pool_connections=0, pool_maxsize=0 sets the RecentlyUsedContainer capacity to 0. It does not close or prevent connections; instead, urllib3 creates a fresh connection pool on each request and succeeds with HTTP 200.

To actually exercise the ClosedPoolError -> ConnectionError wrapping logic in HTTPAdapter.send():

  1. Make an initial request to create the pool in the adapter's poolmanager.
  2. Close the active pool via adapter.poolmanager.pools[key].close().
  3. The subsequent request reuses that pool key, triggering urllib3's ClosedPoolError, which HTTPAdapter wraps into requests.exceptions.ConnectionError("... Pool is closed.").
def test_urllib3_pool_connection_closed(httpbin):
    s = requests.Session()
    adapter = HTTPAdapter()
    s.mount("http://", adapter)
    s.get(httpbin("status/200"))
    for key in list(adapter.poolmanager.pools.keys()):
        adapter.poolmanager.pools[key].close()

    with pytest.raises(ConnectionError) as excinfo:
        s.get(httpbin("status/200"))
    assert "Pool is closed." in str(excinfo.value)

Both tests pass and correctly fail if an exception is not raised. Commit reference: https://github.com/Prognosticate-X/requests/commit/46a920a1

Prognosticate-X avatar Nov 03 '26 22:11 Prognosticate-X