Skip to content

Skip tests that need netCDF4 or h5py instead of failing when they aren't installed - #1087

Draft
TomNicholas wants to merge 2 commits into
zarr-developers:mainfrom
TomNicholas:skip-tests-without-netcdf4
Draft

Skip tests that need netCDF4 or h5py instead of failing when they aren't installed#1087
TomNicholas wants to merge 2 commits into
zarr-developers:mainfrom
TomNicholas:skip-tests-without-netcdf4

Conversation

@TomNicholas

Copy link
Copy Markdown
Member

netCDF4 and h5py are only needed to write the test files — and, for netCDF4, to read xarray's tutorial datasets. Nothing in the library requires them at import time. But both were imported unconditionally at the top of two conftests and two test modules, so without either the suite failed at collection rather than skipping.

Changes

  • netcdf4_lib and h5py_lib fixtures calling pytest.importorskip, which the file-writing fixtures now depend on (2 in the root conftest, 29 in test_parsers/conftest.py).
  • Module-level pytest.importorskip in test_hdf.py and test_hdf_filters.py, which call h5py directly.
  • requires_netcdf4 / requires_h5py markers on tests that read or write these files in their own bodies. That includes everything exercising HDFParser, which needs h5py at runtime via its soft import even when the file was written by netCDF4.
  • string_dtype_hdf5_url is parametrized by name rather than by value: its params=[...] called h5py.string_dtype() at collection time, which no fixture can defer. Test IDs are unchanged.

Suite totals

before after
all installed 649 passed, 25 skipped 649 passed, 25 skipped
no netCDF4 54 failed, 74 errors 516 passed, 166 skipped, 0 failed
no h5py collection aborted 484 passed, 135 skipped, 0 failed
neither collection aborted 446 passed, 174 skipped, 0 failed

Every CI environment installs both through the hdf5-lib and hdf features, so coverage in CI is unchanged — the "all installed" row is the one CI exercises.

Verified by blocking each library at interpreter startup via sitecustomize.py, rather than from within a pytest plugin: virtualizarr.tests is imported before pytest_configure runs, so a plugin-based block leaves has_netcdf4 already computed as True and the markers silently don't fire.

netCDF4 is only ever used to write test files, and to read the tutorial
datasets xarray fetches; nothing under test needs it at runtime. It was
nonetheless imported unconditionally at the top of
virtualizarr/tests/test_parsers/conftest.py, so without it the suite failed at
collection rather than skipping.

Add a netcdf4_lib fixture that calls pytest.importorskip, have every fixture
that writes a netCDF file depend on it, and mark the handful of tests that
write or read netCDF files in their own bodies with requires_netcdf4.

With netCDF4 installed the suite is unchanged at 649 passed / 25 skipped. With
it made unimportable at interpreter startup, the suite now reports 516 passed /
166 skipped with no failures or errors, where it previously reported 54
failures and 74 errors. Every CI environment installs netcdf4 through the
hdf5-lib feature, so coverage there is unaffected.
Same treatment as netCDF4 in the previous commit. h5py is used only to write
the HDF5 test files, and HDFParser reaches it through a soft import, but it was
imported unconditionally at the top of two conftests and two test modules, so
without it the suite failed at collection.

Add an h5py_lib fixture calling pytest.importorskip and route the 29 fixtures
that write HDF5 files through it; use a module-level importorskip in the two
test modules that call h5py directly; and mark the tests that exercise
HDFParser with requires_h5py, since the parser needs h5py at runtime even when
the file was written by something else.

The string_dtype_hdf5_url fixture is now parametrized by name rather than by
value, because its parameter list called h5py.string_dtype() at import time,
which cannot be deferred to a fixture.

Suite totals, unchanged with everything installed and now clean without:
  all installed  649 passed,  25 skipped, 0 failed
  no netCDF4     516 passed, 166 skipped, 0 failed
  no h5py        484 passed, 135 skipped, 0 failed
  neither        446 passed, 174 skipped, 0 failed
@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.67%. Comparing base (7805559) to head (1b3316f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1087   +/-   ##
=======================================
  Coverage   89.67%   89.67%           
=======================================
  Files          41       41           
  Lines        2682     2682           
=======================================
  Hits         2405     2405           
  Misses        277      277           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant