Cantera / cantera

Chemical kinetics, thermodynamics, and transport tool suite
https://cantera.org
Other
582 stars 342 forks source link

Make pint optional when running tests #1525

Closed speth closed 11 months ago

speth commented 1 year ago

Problem description

The use of the Pint units library within the Cantera Python module is optional. However, attempting to run the Python test suite without Pint installed fails.

This is dissimilar to how we handle testing of other optional dependencies in Python, where we are able to use the @pytest.mark.skipif decorator.

Steps to reproduce

  1. Set up a Cantera build environment including all Python dependencies, but not including Pint
  2. Run scons test

Behavior

___________________________________________ ERROR collecting test_units.py ___________________________________________
ImportError while importing test module '/root/cantera/test/python/test_units.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test/python/test_units.py:7: in <module>
    import cantera.with_units as ctu
build/python/cantera/with_units/__init__.py:7: in <module>
    from pint import UnitRegistry, set_application_registry
E   ModuleNotFoundError: No module named 'pint'

System information

ischoegl commented 11 months ago

The bad part here is that the entire Python test suite fails as pint cannot be imported ... @bryanwweber do you have an angle on how to prevent this from happening?

% scons test-python
[...]
=============================================== test session starts ===============================================
platform darwin -- Python 3.10.0, pytest-7.2.2, pluggy-1.2.0
rootdir: /Volumes/Data/work/GitHub/cantera/test/python, configfile: pytest.ini
plugins: anyio-3.7.1
collected 1867 items / 1 error

===================================================== ERRORS ======================================================
_________________________________________ ERROR collecting test_units.py __________________________________________
ImportError while importing test module '/Volumes/Data/work/GitHub/cantera/test/python/test_units.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Caskroom/miniforge/base/envs/cantera-dev/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test/python/test_units.py:7: in <module>
    import cantera.with_units as ctu
build/python/cantera/with_units/__init__.py:7: in <module>
    from pint import UnitRegistry, set_application_registry
E   ImportError: cannot import name 'UnitRegistry' from 'pint' (unknown location)
------------------- generated xml file: /Volumes/Data/work/GitHub/cantera/test/work/pytest.xml --------------------
============================================= short test summary info =============================================
ERROR test/python/test_units.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================ 1 error in 0.38s =================================================
bryanwweber commented 11 months ago

pytest allows you to importorskip, I believe you can set that for the whole module. https://docs.pytest.org/en/7.1.x/how-to/skipping.html#skipping-on-a-missing-import-dependency