Skip to content

Allow skipping test cases that need Internet access #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/16.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Support skipping all test cases that access the Internet by setting the
environment variable `NO_INTERNET`. This is useful to make the test run
reproducible and robust for future runs (to avoid breaking in case some random
service on the Internet changes).
2 changes: 2 additions & 0 deletions src/pytestskipmarkers/downgraded/utils/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def skip_if_no_remote_network() -> Optional[str]:
str: The reason for the skip.
None: Should not be skipped.
"""
if os.environ.get('NO_INTERNET'):
return 'Environment variable NO_INTERNET is set'
has_remote_network = False
for addr in (
'172.217.17.14',
Expand Down
3 changes: 3 additions & 0 deletions src/pytestskipmarkers/utils/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def skip_if_no_remote_network() -> Optional[str]:
str: The reason for the skip.
None: Should not be skipped.
"""
if os.environ.get("NO_INTERNET"):
return "Environment variable NO_INTERNET is set"

# We are using the google.com DNS records as numerical IPs to avoid
# DNS look ups which could greatly slow down this check
has_remote_network = False
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/utils/markers/test_skip_if_no_remote_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
Test the "skip_if_no_remote_network" marker helper.
"""
import os
from unittest import mock

import pytestskipmarkers.utils.markers as markers
Expand All @@ -22,3 +23,10 @@ def test_no_remote_network():
skip_reason = markers.skip_if_no_remote_network()
assert skip_reason is not None
assert skip_reason == "No internet network connection was detected"


def test_remote_network_with_no_internet_env_variable():
with mock.patch.dict(os.environ, {"NO_INTERNET": "1"}):
skip_reason = markers.skip_if_no_remote_network()
assert skip_reason is not None
assert skip_reason == "Environment variable NO_INTERNET is set"