Block network access
masterTo ensure tests do not make real network calls, use the @pytest.mark.block_network decorator or the --block-network CLI flag.
Note: If VCR.py recording is enabled (via @pytest.mark.vcr), network access is not blocked for those specific tests.
Features:
- Supports
socket-based transports andpycurl. - Allows specific hosts via
allowed_hosts.
Usage Examples:
import pytest
import requests
# Block all network access
@pytest.mark.block_network
def test_no_network():
assert requests.get("http://httpbin.org/get")
# Allow specific hosts
@pytest.mark.block_network(allowed_hosts=["httpbin.*"])
def test_partial_access():
assert requests.get("http://httpbin.org/get").text == '{"get": true}'
with pytest.raises(RuntimeError, match=r"^Network is disabled$"):
requests.get("http://example.com")CLI Example:
pytest --record-mode=once --block-network --allowed-hosts=httpbin.*,localhost tests/