pytest-astropy
pytest-astropy is a meta-package that consolidates and provides testing dependencies and various pytest plugins used by the Astropy Project and its affiliated packages. It aims to simplify test setup for projects within the Astropy ecosystem and can also be used by other Python projects. The library is actively maintained, with its current version being 0.11.0, and receives regular updates.
Warnings
- breaking Support for Python 3.6 was dropped in `pytest-astropy` version 0.9.0. Projects using older Python versions will need to upgrade their environment or pin an earlier version of `pytest-astropy`.
- breaking The `pytest-openfiles` plugin was dropped from `pytest-astropy` in version 0.11.0. If your project explicitly relied on `pytest-openfiles` being pulled in via `pytest-astropy`, you will need to install it directly.
- deprecated The Astropy `astropy.test()`, `TestRunner`, and `TestRunnerBase` APIs are deprecated as of Astropy v8.0. This change affects downstream packages that generate `packagename.test` functions using `TestRunner`. It is recommended to switch to using `pytest` natively (e.g., `pytest --pyargs packagename`) instead of `packagename.test()` functions.
- gotcha When migrating older Astropy-affiliated packages, ensure that any `astropy.tests.plugins.display` imports in your `conftest.py` are updated to use `pytest_astropy_header.display`. Older plugin paths are no longer supported by `pytest-astropy-header` for Astropy versions less than 4.
Install
-
pip install pytest-astropy
Imports
- pytest.mark.slow
import pytest @pytest.mark.slow def test_something_slow(): ...
- pytest.mark.hugemem
import pytest @pytest.mark.hugemem def test_something_using_lots_of_memory(): ...
- pytest.mark.remote_data
import pytest @pytest.mark.remote_data def test_requires_internet(): ...
- pytest_configure (in conftest.py)
from pytest_astropy_header.display import PYTEST_HEADER_MODULES, TESTED_VERSIONS def pytest_configure(config): config.option.astropy_header = True PYTEST_HEADER_MODULES['Astropy'] = 'astropy'
Quickstart
# conftest.py
import pytest
from pytest_astropy_header.display import PYTEST_HEADER_MODULES, TESTED_VERSIONS
def pytest_configure(config):
config.option.astropy_header = True
PYTEST_HEADER_MODULES['Astropy'] = 'astropy'
# test_example.py
import pytest
@pytest.mark.slow
def test_a_slow_calculation():
"""This test simulates a slow operation."""
# Simulate a slow operation
sum(range(10**7))
assert True
@pytest.mark.remote_data
def test_fetch_remote_data():
"""This test requires remote data access."""
# Example: This test would typically try to fetch data from the internet.
# It will be skipped by default unless --remote-data is specified.
# import urllib.request
# with urllib.request.urlopen("https://www.google.com") as response:
# assert response.getcode() == 200
assert True # Placeholder for actual remote data logic
# To run the slow test:
# pytest --run-slow
# To run remote data tests:
# pytest --remote-data=any