Skip to content

Commit

Permalink
testing setup fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Jul 30, 2024
1 parent 9eba218 commit 05f08e7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 46 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,26 @@ jobs:
markers: -m 'not slow'
os: macos-latest
# Linux builds
- tox-env: py39-coverage-offline-prefetch
python-version: "3.9"
markers: -m 'not slow and not requires_internet'
os: ubuntu-latest
- tox-env: py310-coverage-lmoments # No markers -- includes slow tests
python-version: "3.10"
os: ubuntu-latest
- tox-env: py311-coverage-extras-sbck
python-version: "3.11"
markers: -m 'not slow'
os: ubuntu-latest
- tox-env: py312-coverage-extras-lmoments-sbck
- tox-env: py312-coverage-extras-lmoments
python-version: "3.12"
markers: -m 'not slow'
os: ubuntu-latest
# Specialized tests
- tox-env: notebooks
python-version: "3.10"
os: ubuntu-latest
- tox-env: offline-prefetch
python-version: "3.11"
markers: -m 'not slow and not requires_internet'
os: ubuntu-latest
- tox-env: doctest
- tox-env: doctests
python-version: "3.12"
os: ubuntu-latest
steps:
Expand Down
7 changes: 3 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from xclim.core import indicator
from xclim.core.calendar import max_doy
from xclim.testing import helpers
from xclim.testing.helpers import setup_warnings, test_timeseries
from xclim.testing.helpers import test_timeseries, testing_setup_warnings
from xclim.testing.utils import _default_cache_dir # noqa
from xclim.testing.utils import get_file
from xclim.testing.utils import open_dataset as _open_dataset
Expand Down Expand Up @@ -396,10 +396,9 @@ def gather_session_data(threadsafe_data_dir, worker_id):
other workers wait using lockfile. Once the lock is released, all workers will then copy data to their local
threadsafe_data_dir.As this fixture is scoped to the session, it will only run once per pytest run.
Additionally, this fixture is also used to generate the `atmosds` synthetic testing dataset as well as add the
example file paths to the xdoctest_namespace, used when running doctests.
Additionally, this fixture is also used to generate the `atmosds` synthetic testing dataset.
"""
setup_warnings()
testing_setup_warnings()

if (
not _default_cache_dir.joinpath(helpers.TESTDATA_BRANCH).exists()
Expand Down
29 changes: 12 additions & 17 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ env_list =
lint
docs
notebooks
offline-prefetch
doctests
py39-upstream-doctest
py310-doctest
py311-lmoments
py312-lmoments-doctest
labels =
test = py39, py310-upstream-doctest, py311, notebooks_doctests, offline-prefetch
requires =
Expand All @@ -18,10 +17,10 @@ opts = -vv

[gh]
python =
3.12 = py312-coverage-lmoments-doctest
3.11 = py311-coverage-lmoments-sbck-doctest, offline-coverage-prefetch
3.10 = py310-coverage-lmoments-doctest, notebooks
3.9 = py39-coverage-sbck-doctest, lint, docs
3.12 = doctests
3.11 = py311-coverage-lmoments-sbck, offline-coverage-prefetch
3.10 = py310-coverage-lmoments, notebooks
3.9 = py39-coverage-sbck, lint, docs

[testenv:lint]
description = Run code quality compliance tests under {basepython}
Expand Down Expand Up @@ -88,19 +87,11 @@ commands =
pytest --no-cov --nbval --dist=loadscope --rootdir=tests/ --ignore=docs/notebooks/example.ipynb docs/notebooks
commands_post =

[testenv:offline{-prefetch,}{-coverage,}]
description = Run tests with pytest under {basepython}, preventing socket connections (except for unix sockets for async support)
commands:
python -c 'print("Running offline tests with positional arguments: --disable-socket --allow-unix-socket --m \"not requires_internet\"")'
python -c 'print("These can be overwritten with: tox -e offline -- -m \"some other marker statement\"")'
pytest --disable-socket --allow-unix-socket {posargs:-m 'not requires_internet'}
allowlist_externals =
xclim

[testenv:doctest]
[testenv:doctests]
description = Run doctests with pytest under {basepython}
commands =
python -c 'from xclim.testing import helpers; helpers.test_doctests()'
commands_post =

[testenv]
description = Run tests with pytest under {basepython}
Expand Down Expand Up @@ -135,7 +126,11 @@ commands_pre =
xclim --help
prefetch: xclim prefetch_testing_data
commands =
pytest {posargs}
!offline: pytest {posargs}
; Prevent socket connections (except for unix sockets for async support)
offline: python -c 'print("Running offline tests with positional arguments: --disable-socket --allow-unix-socket --m \"not requires_internet\"")'
offline: python -c 'print("These can be overwritten with: tox -e offline -- -m \"some other marker statement\"")'
offline: pytest --disable-socket --allow-unix-socket {posargs:-m 'not requires_internet'}
commands_post =
coverage: - coveralls
allowlist_externals =
Expand Down
4 changes: 2 additions & 2 deletions xclim/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def _is_matplotlib_installed():

@pytest.fixture(autouse=True, scope="session")
def add_doctest_imports(xdoctest_namespace, threadsafe_data_dir) -> None:
"""Add these imports into the doctests scope."""
helpers.setup_warnings()
"""Gather testing data on doctest run."""
helpers.testing_setup_warnings()

shutil.copytree(_default_cache_dir, threadsafe_data_dir, dirs_exist_ok=True)
helpers.generate_atmos(threadsafe_data_dir)
Expand Down
18 changes: 1 addition & 17 deletions xclim/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
import re
import sys
import time
import warnings
from datetime import datetime as dt
Expand Down Expand Up @@ -77,26 +76,11 @@
"assert_lazy",
"generate_atmos",
"populate_testing_data",
"run_doctests",
"test_timeseries",
]


def run_doctests():
"""Run the doctests for the module."""
import pytest

cmd = [
f"--rootdir={Path(__file__).absolute().parent}",
"--numprocesses=0",
"--xdoctest",
f"{Path(__file__).absolute().parents[1]}",
]

sys.exit(pytest.main(cmd))


def setup_warnings():
def testing_setup_warnings():
"""Warn users about potential incompatibilities between xclim and xclim-testdata versions."""
if re.match(r"^\d+\.\d+\.\d+$", __xclim_version__) and TESTDATA_BRANCH == "main":
# This does not need to be emitted on GitHub Workflows and ReadTheDocs
Expand Down
15 changes: 15 additions & 0 deletions xclim/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"list_input_variables",
"open_dataset",
"publish_release_notes",
"run_doctests",
"show_versions",
]

Expand Down Expand Up @@ -476,6 +477,20 @@ def list_input_variables(
return variables


def run_doctests():
"""Run the doctests for the module."""
import pytest

cmd = [
f"--rootdir={Path(__file__).absolute().parent}",
"--numprocesses=0",
"--xdoctest",
f"{Path(__file__).absolute().parents[1]}",
]

sys.exit(pytest.main(cmd))


def publish_release_notes(
style: str = "md",
file: os.PathLike | StringIO | TextIO | None = None,
Expand Down

0 comments on commit 05f08e7

Please sign in to comment.