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

Pytest transition #1793

Merged
merged 48 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
222f042
[test] first pass at pytest structure for tests
wandadars Sep 22, 2024
81fe0d8
[test] updated test_equilibrium to use pytest fully
wandadars Sep 22, 2024
b12ebbf
[test] updated test_utils to use pytest fully
wandadars Sep 22, 2024
eb0a53a
[test] updated test_composite to use pytest fully
wandadars Sep 22, 2024
2fef131
[test] updated test_reactor to use pytest fully
wandadars Sep 22, 2024
b9fe294
[test] updated test_jacobian to use pytest fully
wandadars Sep 22, 2024
ef8d748
[test] updated test_convert to use pytest fully
wandadars Sep 22, 2024
99b8b6f
[test] updated test_mixture to use pytest fully
wandadars Sep 22, 2024
c889d1a
[test] updated test_kinetics to use pytest fully
wandadars Sep 22, 2024
19d2035
[test] updated test_transport to use pytest fully
wandadars Sep 22, 2024
546860c
[test] updated test_onedim to use pytest fully
wandadars Sep 23, 2024
849ccd1
[test] updated test_reaction to use pytest fully
wandadars Sep 23, 2024
b084204
updated test_purefluid to use pytest fully
wandadars Sep 23, 2024
6216af5
[test] updated test_thermo to use pytest fully
wandadars Sep 23, 2024
a6ae8c7
more updates to test_func1 to use pytest
wandadars Sep 26, 2024
56c9b2d
Update Pytest configuration and slow test handling
wandadars Sep 27, 2024
f205379
removed assertIsNan and assertIsFinite methods
wandadars Sep 28, 2024
0762dca
[test] Replaced remaining unittest-style setup with pytest fixtures
wandadars Sep 29, 2024
adfb15f
[test] removed outdated python version check from test_composite
wandadars Sep 29, 2024
faeb465
[test] updated test_composite to replace assertNear with pytest approx
wandadars Sep 29, 2024
7375644
[test] updated test_convert to replace assertNear with pytest approx
wandadars Sep 29, 2024
ab076f2
[test] added test_data_path and cantera_data_path fixtures for future…
wandadars Sep 30, 2024
bab1083
[test] updated test_equilibrium to replace assertNear with pytest approx
wandadars Sep 30, 2024
57baf44
[test] updated test_func1 to replace assertNear with pytest approx
wandadars Sep 30, 2024
7057acb
[test] updated test_jacobian to replace assertNear with pytest approx
wandadars Sep 30, 2024
5728f15
[test] updated test_kinetics to replace assertNear with pytest approx
wandadars Oct 1, 2024
1af83ac
[test] updated test_mixture to replace assertNear with pytest approx
wandadars Oct 1, 2024
cb10d55
[test] updated test_onedim to replace assertNear with pytest approx
wandadars Oct 1, 2024
74ecc20
[test] updated test_purefluid to replace assertNear with pytest approx
wandadars Oct 1, 2024
178a352
[test] updated test_reaction to replace assertNear with pytest approx
wandadars Oct 1, 2024
9c82e6c
[test] updated test_reactor to replace assertNear with pytest approx
wandadars Oct 1, 2024
e37c765
[test] updated test_thermo to replace assertNear with pytest approx
wandadars Oct 2, 2024
733e51d
[test] updated test_transport to replace assertNear with pytest approx
wandadars Oct 2, 2024
b83dcef
[test] updated test_composite to replace assertArrayNear with pytest …
wandadars Oct 2, 2024
cffabca
[test] Replace assertArrayNear with pytest approx
wandadars Oct 2, 2024
8c9c802
[test] Restore load_yaml function; add fixtures for paths
wandadars Oct 7, 2024
69bb8b8
[test] moved to test_data_path fixture where possible
wandadars Oct 8, 2024
6cfb761
[test] removed class-attribute setting in jacobian tests to avoid a C…
wandadars Oct 10, 2024
b0e2780
[test] changed test_mixture to avoid using/setting class attributes
wandadars Oct 10, 2024
2912fdb
[test] Removed class attribute usage in test_transport
wandadars Oct 10, 2024
f526576
[test] increased FallOff reaction test relative tolerance
wandadars Oct 13, 2024
5dcf705
[test] removed the class attribute setting in test_composite
wandadars Oct 13, 2024
0ed80e9
[test] reduced usage of class attributes in test_kinetics
wandadars Oct 13, 2024
6504c41
[test] reduced usage of class attributes in test_reaction
wandadars Oct 13, 2024
2902016
[test] addressing review comments
wandadars Oct 17, 2024
55fc1f0
[test] formatting updates to test_reaction.py
wandadars Oct 18, 2024
52fbb27
[test] updated tests that generated reference files to be run-able wi…
wandadars Oct 19, 2024
12ce03f
[test] removed useless fixtures in test_transport
wandadars Oct 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions test/python/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,119 @@
import cantera
import os
import sys
from pathlib import Path
import pytest
import tempfile

try:
from ruamel import yaml
except ImportError:
import ruamel_yaml as yaml

pytest.register_assert_rewrite("pint.testing")

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


def pytest_addoption(parser):
parser.addoption(
"--save-reference", action="store", default=None,
help="Save the reference output files for specific tests. "
"Options: diffusion, counterflow_premixed, counterflow_premixed_nonideal, "
"combustor, wall"
)

def pytest_configure(config):
config.addinivalue_line("markers", "slow_test: mark test as slow")

def pytest_collection_modifyitems(config, items):
if os.environ.get("CT_SKIP_SLOW", "0") == "1":
skip_slow = pytest.mark.skip(reason="slow test")
for item in items:
if "slow_test" in item.keywords:
item.add_marker(skip_slow)

@pytest.fixture
def allow_deprecated():
cantera.suppress_deprecation_warnings()
yield
cantera.make_deprecation_warnings_fatal()


@pytest.fixture
def has_temperature_derivative_warnings():
with pytest.warns(UserWarning, match="ddTScaledFromStruct"):
# test warning raised for BlowersMasel and TwoTempPlasma derivatives
yield

@pytest.fixture(scope="session")
def test_data_path():
return TEST_DATA_PATH

@pytest.fixture(scope="session")
def cantera_data_path():
return CANTERA_DATA_PATH

@pytest.fixture(scope="module", autouse=True)
def cantera_setup():
"""
Fixture to set up Cantera environment for the entire test session.
"""
# Add data directories
cantera.add_directory(TEST_DATA_PATH)
cantera.add_directory(CANTERA_DATA_PATH)
cantera.print_stack_trace_on_segfault()
cantera.CanteraError.set_stack_trace_depth(20)
cantera.make_deprecation_warnings_fatal()

# Yield control to tests
yield


@pytest.fixture(scope="class", autouse=True)
def test_work_path(request, cantera_setup):
"""
Fixture to create a working directory for a test class.
This will only run for class-based tests.

The check on the request.cls attribute is to ensure that this fixture
does not run for function-based tests. There is likely a better way to
do this, perhaps by disabling autouse and using the fixture explicitly
in the test classes that need it.
"""

if request.cls is None:
# If this is not a class-based test, do nothing.
yield
return

root_dir = Path(__file__).parents[2].resolve()
if (root_dir / "SConstruct").is_file():
work_path = root_dir / "test" / "work" / "python"
using_tempfile = False
try:
work_path.mkdir(exist_ok=True)
except FileNotFoundError:
work_path = Path(tempfile.mkdtemp())
using_tempfile = True
else:
work_path = Path(tempfile.mkdtemp())
using_tempfile = True
Comment on lines +92 to +102
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than setting it up this way, I think we should directly use the built-in tmp_path fixture. I don't think there's a reason to store temp files in the working tree anymore?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a significant benefit to the default behavior of using a directory within the source tree like test/work/python, which is that it makes it much easier to debug certain failing tests when you can inspect the temporary files that are being generated.


cantera.add_directory(work_path)

# Assign to the test class
request.cls.test_work_path = work_path
request.cls.using_tempfile = using_tempfile

yield

# Teardown: Remove the working directory if it was a temporary one
if using_tempfile:
try:
for f in work_path.glob("*.*"):
f.unlink()
work_path.rmdir()
except FileNotFoundError:
pass
3 changes: 3 additions & 0 deletions test/python/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[pytest]
markers =
slow_test: mark test as slow

pythonpath = ../../build/python
Loading
Loading