Skip to content

Commit

Permalink
Honor 'network' marker to signal network-dependent tests. Fixes #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 31, 2022
1 parent 8e47a5f commit fbbad0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v5.3.0
======

#5: Plugin now honors the ``network`` marker convention for
tagging network-required tests.

v5.2.0
======

Expand Down
17 changes: 17 additions & 0 deletions jaraco/test/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ def check_internet():
@pytest.fixture
def needs_internet():
check_internet()


def pytest_configure(config):
"""
Register the 'network' marker.
"""
config.addinivalue_line(
"markers", "network: the test requires network connectivity"
)


def pytest_runtest_setup(item):
"""
For any tests marked with 'network', install fixture.
"""
for marker in item.iter_markers(name='network'):
item.fixturenames.extend({'needs_internet'} - set(item.fixturenames))
10 changes: 10 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import urllib.request

import pytest


def test_needs_internet(needs_internet):
"""
This test should always succeed or be skipped.
"""
urllib.request.urlopen('http://pypi.org/')


@pytest.mark.network
def test_network_marker():
"""
This test should always succeed or be skipped.
"""
urllib.request.urlopen('http://pypi.org/')

1 comment on commit fbbad0e

@mgorny
Copy link

@mgorny mgorny commented on fbbad0e Jan 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this! I dare say it is a great thing — it works out of the box for "unaware" builds, and lets us cleanly block Internet access entirely via -m "not network".

Please sign in to comment.