Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Python test suite #1354

Merged
merged 4 commits into from Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions interfaces/cython/SConscript
Expand Up @@ -12,9 +12,6 @@ localenv = env.Clone()
dataFiles = localenv.RecursiveInstall("cantera/data", "#build/data")
build(dataFiles)

testFiles = localenv.RecursiveInstall("cantera/test/data", "#test/data")
build(testFiles)

# Get information needed to build the Python module
script = """\
from sysconfig import *
Expand Down Expand Up @@ -116,13 +113,12 @@ mod = build(localenv.Command(f"#build/python/dist/{wheel_name}", "setup.cfg",
env['python_module'] = mod
env['python_extension'] = ext

localenv.Depends(mod, [ext, dataFiles, testFiles, setup_cfg, readme, license,
"setup.py", "pyproject.toml"])
localenv.Depends(mod, [ext, dataFiles, setup_cfg, readme, license,
"setup.py", "pyproject.toml", "cantera/test/README.txt"])
localenv.Depends(ext, localenv['cantera_staticlib'])

for f in (multi_glob(localenv, 'cantera', 'py') +
multi_glob(localenv, 'cantera/*', 'py') +
multi_glob(localenv, 'cantera/*/*', 'py')):
multi_glob(localenv, 'cantera/*', 'py')):
localenv.Depends(mod, f)

# Determine installation path and install the Python module
Expand Down
4 changes: 4 additions & 0 deletions interfaces/cython/cantera/test/README.txt
@@ -0,0 +1,4 @@
Starting with Cantera 3.0, the Python test suite is no longer packaged with the Python
module. Unit tests can be accessed in the directory "test/python" of the source code,
which can be retrieved from the project's GitHub repository
(https://github.com/Cantera/cantera).
20 changes: 0 additions & 20 deletions interfaces/cython/cantera/test/__init__.py

This file was deleted.

5 changes: 1 addition & 4 deletions interfaces/cython/setup.cfg.in
Expand Up @@ -47,18 +47,15 @@ python_requires = >=@py_min_ver_str@
packages =
cantera
cantera.data
cantera.test
cantera.test.data
cantera.examples

[options.package_data]
cantera.data = *.*, */*.*
cantera.test.data = *.*, */*.*
cantera.examples = */*.*
# The module extension needs to be here since we don't want setuptools to
# compile the extension, so there are no `source` files in
# the setup.py extension.
cantera = *.pxd, *@module_ext@
cantera = *.pxd, *@module_ext@, test/*.txt

[options.extras_require]
hdf5 = h5py
Expand Down
3 changes: 0 additions & 3 deletions interfaces/python_sdist/setup.cfg.in
Expand Up @@ -47,16 +47,13 @@ python_requires = >=@py_min_ver_str@
packages =
cantera
cantera.data
cantera.test
cantera.test.data
cantera.examples

# These options include data in the sdist and wheel if the files are also listed in
# MANIFEST.in. Note that only files that are inside the "cantera" packages will be
# included in the wheel.
[options.package_data]
cantera.data = *.*, */*.*
cantera.test.data = *.*, */*.*
cantera.examples = */*.*
cantera = *.pxd

Expand Down
2 changes: 1 addition & 1 deletion test/SConscript
Expand Up @@ -292,7 +292,7 @@ addTestProgram('transport', 'transport')
addTestProgram('zeroD', 'zeroD')

python_subtests = ['']
test_root = '#interfaces/cython/cantera/test'
test_root = '#test/python'
for f in multi_glob(localenv, test_root, '^test_*.py'):
python_subtests.append(f.name[5:-3])

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions test/python/pytest.ini
@@ -0,0 +1,2 @@
[pytest]
pythonpath = ../../build/python
34 changes: 24 additions & 10 deletions test/python/runCythonTests.py
@@ -1,21 +1,36 @@
"""
Unit tests for Cantera's Cython-based Python module.

This script gathers all the tests defined 'cantera.test' module, runs them,
and prints a report. Extra command line arguments can be used to run subsets
of the test suite, for example:
This script is used by SCons to run the Python test suite, where ``pytest`` is used
as the test runner. All arguments are handled by SCons configurations.

all tests from 'test_thermo.py' and 'test_kinetics.py':
This script gathers all Python tests, runs them, and prints a report::

python runCythonTests.py

Extra command line arguments can be used to run subsets of the test suite, for example
all tests from ``test_thermo.py`` and ``test_kinetics.py``::

python runCythonTests.py thermo kinetics

all tests from the 'test_reactor.TesTIdealGasReactor' class:
As an alternative, tests can be run using ``pytest`` directly, as illustrated for the
following examples (run from Cantera's root folder):

all tests::

pytest test/python

all tests from ``test_transport.py``::

pytest test/python/test_transport.py

all tests from the ``test_reactor.TestIdealGasReactor`` class::

python runCythonTests.py reactor.TestIdealGasReactor
pytest test/python/test_reactor.py::TestIdealGasReactor

a single test:
a single test::

python runCythonTests.py onedim.TestDiffusionFlame.test_mixture_averaged
pytest test/python/test_onedim.py::TestDiffusionFlame::test_mixture_averaged
"""

import sys
Expand All @@ -35,7 +50,6 @@
sys.exit(21) # test/SConscript has special handling for this error code

import cantera
import cantera.test

if __name__ == "__main__":
print("\n* INFO: using Cantera module found at this location:")
Expand All @@ -58,7 +72,7 @@
verbose = True
subset_start += 1

base = Path(cantera.__file__).parent.joinpath("test")
base = CANTERA_ROOT / "test" / "python"
subsets = []
for name in sys.argv[subset_start:]:
subsets.append(str(base.joinpath(f"test_{name}.py")))
Expand Down
Expand Up @@ -517,7 +517,7 @@ def test_error_for_big_element_number(self):

class yaml2ckTest(utilities.CanteraTest):
"""Test yaml2ck by converting to CK then back to YAML to read with Cantera."""
ext: str = ".yaml2ck.yaml"
ext: str = "-from-yaml2ck.yaml"

def _convert_to_ck(
self,
Expand All @@ -527,7 +527,8 @@ def _convert_to_ck(
) -> tuple[Path | None, Path | None, Path | None]:
mechanism_path: Path | str
if not output:
mechanism_path = input_file.with_suffix(".ck")
stem = Path(input_file).stem # strip '.inp'
mechanism_path = self.test_work_path / (stem + "-from-yaml.ck")
thermo_path = transport_path = None
else:
if len(output) != 3:
Expand Down Expand Up @@ -559,12 +560,16 @@ def convert(
transport: str | Path | None = None,
permissive: bool = False,
) -> None:
mech, thermo, transport = self._convert_to_ck(
input_file,
phase_name,
(mech, thermo, transport),
)
output = input_file.with_suffix(self.ext)
if mech is not None:
mech, thermo, transport = self._convert_to_ck(
input_file,
phase_name,
(mech, thermo, transport),
)
else:
mech, thermo, transport = self._convert_to_ck(input_file, phase_name)

output = self.test_work_path / (Path(input_file).stem + self.ext)
ck2yaml.convert_mech(
mech,
thermo_file=thermo,
Expand All @@ -581,7 +586,8 @@ def check_conversion(self, basename, cls=ct.Solution, **kwargs):
phase_name = kwargs.pop("name")
else:
phase_name = ""
ck_phase = cls(basename.with_suffix(self.ext), **kwargs)
ckname = self.test_work_path / (basename.stem + self.ext)
ck_phase = cls(ckname, **kwargs)
yaml_phase = cls(basename, phase_name, **kwargs)

self.assertEqual(set(ck_phase.element_names), set(yaml_phase.element_names))
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -15,9 +15,11 @@

slow_test = unittest.skipIf(environ.get("CT_SKIP_SLOW", "0") == "1", "slow test")

TEST_DATA_PATH = Path(__file__).parent / "data"
CANTERA_DATA_PATH = Path(__file__).parents[1] / "data"
TEST_DATA_PATH = Path(__file__).parents[1] / "data"
CANTERA_DATA_PATH = Path(__file__).parents[2] / "data"

cantera.add_directory(TEST_DATA_PATH)
cantera.add_directory(CANTERA_DATA_PATH)

@pytest.fixture
def allow_deprecated():
Expand Down