Pytest Remote Data Plugin

0.4.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

To use `pytest-remotedata`, you mark your test functions with `@pytest.mark.remote_data` for tests requiring internet access or `@pytest.mark.internet_off` for tests that expect no internet. When `pytest` is run, tests marked with `remote_data` are skipped by default. They will only execute if the `--remote-data` command-line option (or `--remote-data=any`) is provided to `pytest`. Conversely, tests marked with `internet_off` will run when `--remote-data` is not provided (or `--remote-data=none`), and be skipped if `--remote-data` is active.

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)

view raw JSON →