Skip to content

Commit 8b6960f

Browse files
bdrungs0undt3ch
andcommitted
Allow skipping test cases that need Internet access
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). Signed-off-by: Benjamin Drung <[email protected]> Co-authored-by: Pedro Algarvio <[email protected]>
1 parent 04325d9 commit 8b6960f

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

changelog/16.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Support skipping all test cases that access the Internet by setting the
2+
environment variable `NO_INTERNET`. This is useful to make the test run
3+
reproducible and robust for future runs (to avoid breaking in case some random
4+
service on the Internet changes).

src/pytestskipmarkers/downgraded/utils/markers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def skip_if_no_remote_network() -> Optional[str]:
137137
str: The reason for the skip.
138138
None: Should not be skipped.
139139
"""
140+
if os.environ.get('NO_INTERNET'):
141+
return 'Environment variable NO_INTERNET is set'
140142
has_remote_network = False
141143
for addr in (
142144
'172.217.17.14',

src/pytestskipmarkers/utils/markers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ def skip_if_no_remote_network() -> Optional[str]:
137137
str: The reason for the skip.
138138
None: Should not be skipped.
139139
"""
140+
if os.environ.get("NO_INTERNET"):
141+
return "Environment variable NO_INTERNET is set"
142+
140143
# We are using the google.com DNS records as numerical IPs to avoid
141144
# DNS look ups which could greatly slow down this check
142145
has_remote_network = False

tests/unit/utils/markers/test_skip_if_no_remote_network.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
Test the "skip_if_no_remote_network" marker helper.
66
"""
7+
import os
78
from unittest import mock
89

910
import pytestskipmarkers.utils.markers as markers
@@ -22,3 +23,10 @@ def test_no_remote_network():
2223
skip_reason = markers.skip_if_no_remote_network()
2324
assert skip_reason is not None
2425
assert skip_reason == "No internet network connection was detected"
26+
27+
28+
def test_remote_network_with_no_internet_env_variable():
29+
with mock.patch.dict(os.environ, {"NO_INTERNET": "1"}):
30+
skip_reason = markers.skip_if_no_remote_network()
31+
assert skip_reason is not None
32+
assert skip_reason == "Environment variable NO_INTERNET is set"

0 commit comments

Comments
 (0)