def requires_network() -> typing.Callable[[_TestFuncT], _TestFuncT]:
    """Helps you skip tests that require the network"""

    def _is_unreachable_err(err: Exception) -> bool:
        return getattr(err, "errno", None) in (
            errno.ENETUNREACH,
            errno.EHOSTUNREACH,  # For OSX
        )

    def _has_route() -> bool:
        try:
            sock = socket.create_connection((TARPIT_HOST, 80), 0.0001)
            sock.close()
            return True
        except socket.timeout:
            return True
        except OSError as e:
            if _is_unreachable_err(e):
                return False
            else:
                raise

    def _skip_if_no_route(f: _TestFuncT) -> _TestFuncT:
        """Skip test exuction if network is unreachable"""

        @wraps(f)
        def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
            global _requires_network_has_route
            if _requires_network_has_route is None:
                _requires_network_has_route = _has_route()
            if not _requires_network_has_route:
                pytest.skip("Can't run the test because the network is unreachable")
            return f(*args, **kwargs)

        return typing.cast(_TestFuncT, wrapper)

    def _decorator_requires_internet(
        decorator: typing.Callable[[_TestFuncT], _TestFuncT]
    ) -> typing.Callable[[_TestFuncT], _TestFuncT]:
        """Mark a decorator with the "requires_internet" mark"""

        def wrapper(f: _TestFuncT) -> typing.Any:
            return pytest.mark.requires_network(decorator(f))

        return wrapper

    return _decorator_requires_internet(_skip_if_no_route)
Helps you skip tests that require the network
test/__init__.py