Pytest Remote Data Plugin
Pytest-remotedata is a pytest plugin that provides control over unit tests requiring access to remote data from the internet. It allows developers to mark tests that either need remote data or should only run when internet access is disabled, helping to manage test suite runtime and dependencies. Originally part of the Astropy core package, it is currently at version 0.4.1 and sees active development with a consistent release cadence.
Warnings
- breaking The `-R` command-line option, introduced in `v0.4.0` for marking remote-data tests, was removed in `v0.4.1` as a workaround. Users upgrading from `v0.4.0` to `v0.4.1` should be aware that this option is no longer available.
- gotcha By default, tests marked with `@pytest.mark.remote_data` are skipped unless `pytest` is explicitly invoked with `--remote-data` or `--remote-data=any`. Users might mistakenly assume these tests will run when an internet connection is available without providing the flag.
- gotcha Enabling strict remote data access by setting `remote_data_strict = true` in your `setup.cfg` will cause any test that attempts network access (e.g., via `urlopen`) to fail if it is *not* marked with `@pytest.mark.remote_data`. This is a powerful feature for catching unintended network calls but can lead to unexpected test failures if not understood.
Install
-
pip install pytest-remotedata
Imports
- remote_data
@pytest.mark.remote_data
- internet_off
@pytest.mark.internet_off
Quickstart
import pytest
from urllib.request import urlopen
import os
@pytest.mark.remote_data
def test_access_remote_data():
# This test will only run if pytest is invoked with --remote-data
try:
response = urlopen('https://httpbin.org/get', timeout=5)
assert response.status == 200
print("Successfully accessed remote data.")
except Exception as e:
pytest.fail(f"Failed to access remote data: {e}")
@pytest.mark.internet_off
def test_no_internet_access():
# This test will run if pytest is invoked without --remote-data or with --remote-data=none
# and should ideally not attempt network access.
# For demonstration, we simulate no internet by trying to connect to a non-existent local address.
import socket
with pytest.raises(socket.error): # Expect a connection error if truly offline
socket.create_connection(('localhost', 65535), timeout=0.1)
print("Test passed when internet access is (simulated) off.")
# To run the remote data test:
# pytest --remote-data your_test_file.py
#
# To run the internet_off test (and skip remote_data tests):
# pytest your_test_file.py (or pytest --remote-data=none your_test_file.py)