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

Open
wants to merge 82 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
5ff8d7f
[test] first pass at pytest structure for tests
wandadars Sep 22, 2024
368bf44
revert back to original coding in utilities module
wandadars Sep 22, 2024
0c28847
changed function names to pytest accepted names and disabled default …
wandadars Sep 22, 2024
d250263
[test] updated test_equilibrium to use pytest fully
wandadars Sep 22, 2024
feee3a1
[test] updated test_utils to use pytest fully
wandadars Sep 22, 2024
501bde4
[test] updated test_composite to use pytest fully
wandadars Sep 22, 2024
85166f2
activated the xunit feature again
wandadars Sep 22, 2024
df01b40
[test] fixed a bug I introduced when changing setUp to setup_class, s…
wandadars Sep 22, 2024
5d543d9
[test] updated test_reactor to use pytest fully, changed a setup meth…
wandadars Sep 22, 2024
4d28f88
[test] updated test_jacobian to use pytest fully
wandadars Sep 22, 2024
51b22e4
[test] updated test_convert to use pytest fully
wandadars Sep 22, 2024
c16a19b
[test] updated test_mixture to use pytest fully
wandadars Sep 22, 2024
867d938
[test] updated test_kinetics to use pytest fully
wandadars Sep 22, 2024
6ad7717
[test] updated test_transport to use pytest fully
wandadars Sep 22, 2024
ee74cc1
[test] updated test_onedim to use pytest fully
wandadars Sep 23, 2024
8735f28
[test] updated test_reaction to use pytest fully
wandadars Sep 23, 2024
f0b6260
updated test_purefluid to use pytest fully
wandadars Sep 23, 2024
757ba2f
[test] updated test_thermo to use pytest fully
wandadars Sep 23, 2024
2033a48
[test] fixed erroneous test assertion
wandadars Sep 23, 2024
2be2883
added the utilities module to have fixtures autoloaded
wandadars Sep 26, 2024
547de2d
added fixtures and moved assertions up into module level
wandadars Sep 26, 2024
284c144
more updates to test_utils to use pytest
wandadars Sep 26, 2024
b9a5597
more updates to test_transport to use pytest
wandadars Sep 26, 2024
121425b
more updates to test_thermo to use pytest
wandadars Sep 26, 2024
32cbb56
more updates to test_reactor to use pytest
wandadars Sep 26, 2024
ad98b8e
more updates to test_reaction to use pytest
wandadars Sep 26, 2024
a873457
more updates to test_purefluid to use pytest
wandadars Sep 26, 2024
2833938
more updates to test_onedim to use pytest
wandadars Sep 26, 2024
348aace
more updates to test_mixture to use pytest
wandadars Sep 26, 2024
5b83f96
more updates to test_kinetics to use pytest
wandadars Sep 26, 2024
7434e33
more updates to test_jacobian to use pytest
wandadars Sep 26, 2024
ff82d13
more updates to test_func1 to use pytest
wandadars Sep 26, 2024
7811d79
more updates to test_equilibrium to use pytest
wandadars Sep 26, 2024
0dfa4b2
more updates to test_convert to use pytest
wandadars Sep 26, 2024
371616c
more updates to test_composite to use pytest
wandadars Sep 26, 2024
93180ad
[test] removed old CanteraTest class that was in file as a comment
wandadars Sep 26, 2024
9f8d301
[test] explicitly imported compareProfiles function in test_onedim
wandadars Sep 26, 2024
d5b4466
[test] fixed linter whitespace issues
wandadars Sep 26, 2024
404ffba
[test] fixed more whitespace issues
wandadars Sep 27, 2024
0011c52
[test] missed a section in test_jacobian that needed to be updated to…
wandadars Sep 27, 2024
a55f0d8
[test] pytest.ini whitespace issue
wandadars Sep 27, 2024
e72030a
adding test path to python path so github CI will find the utilities …
wandadars Sep 27, 2024
acbe2eb
updates to move session level fixtures to conftest.py, cleanups of un…
wandadars Sep 27, 2024
6883ff7
[test] restored accidentally removed tempfile import
wandadars Sep 27, 2024
2b36ee5
[test] missed an old unittest that was hidden in a slow test
wandadars Sep 27, 2024
7758522
[test] attempt to fix CI crashes on GitHub
wandadars Sep 27, 2024
ef2cb46
[test] another adjustment for CI issues
wandadars Sep 27, 2024
d1163d7
removed assertIsNan and assertIsFinite methods with equivalent pytest…
wandadars Sep 28, 2024
da15ca7
[test] reverted a removed version note comment
wandadars Sep 28, 2024
e0bd55b
[test] fixed pytest.raises line-break indentation
wandadars Sep 28, 2024
47a4790
removed base class (), and reverted a change that tried to remedy fai…
wandadars Sep 28, 2024
b821a5f
[test] removed remaining unittest-style setup method and replace with…
wandadars Sep 29, 2024
873d029
[test] removed outdated python version check from test_composite
wandadars Sep 29, 2024
06b3ca7
[test] fixed bug in setup fixture, and removed unused load_yaml pytes…
wandadars Sep 29, 2024
195ecc8
[test] changed remaining classes to start with Test so that they are …
wandadars Sep 29, 2024
8313a32
[test] removed unused import
wandadars Sep 29, 2024
77f19d1
[test] fixed a broken assertion and a incorrect fixture scope
wandadars Sep 29, 2024
bcaf360
[test] updated test_composite to replace assertNear with pytest approx
wandadars Sep 29, 2024
fe3dc00
[test] updated test_convert to replace assertNear with pytest approx
wandadars Sep 29, 2024
d442ecf
[test] fixed incorrectly translated assertion
wandadars Sep 30, 2024
23efdf1
[test] fixed erroneous base/derived class relation in kinetics tests
wandadars Sep 30, 2024
d02bba9
[test] added test_data_path and cantera_data_path fixtures for future…
wandadars Sep 30, 2024
9b06dad
[test] fixed mistake where the assert was not added when changing to …
wandadars Sep 30, 2024
6196615
[test] updated test_equilibrium to replace assertNear with pytest approx
wandadars Sep 30, 2024
715d17f
[test] updated test_func1 to replace assertNear with pytest approx
wandadars Sep 30, 2024
5b647c0
[test] updated test_jacobian to replace assertNear with pytest approx
wandadars Sep 30, 2024
590cdf1
[test] fixed missing rel= argument in approx translations
wandadars Oct 1, 2024
97a1df7
[test] updated test_kinetics to replace assertNear with pytest approx
wandadars Oct 1, 2024
7ed666b
[test] updated test_mixture to replace assertNear with pytest approx
wandadars Oct 1, 2024
f4dff24
[test] updated test_onedim to replace assertNear with pytest approx
wandadars Oct 1, 2024
ee3ddc0
[test] updated test_purefluid to replace assertNear with pytest approx
wandadars Oct 1, 2024
a2e8aac
[test] updated test_reaction to replace assertNear with pytest approx
wandadars Oct 1, 2024
e45d53e
[test] replaced pytest.approx with approx
wandadars Oct 1, 2024
ea2ff98
[test] updated test_reactor to replace assertNear with pytest approx
wandadars Oct 1, 2024
6045a03
[test] updated test_thermo to replace assertNear with pytest approx
wandadars Oct 2, 2024
ed2206a
[test] updated test_transport to replace assertNear with pytest approx
wandadars Oct 2, 2024
4a3ba02
[test] removed the assertNear() function from utiliites.py
wandadars Oct 2, 2024
a46512f
[test] updated test_composite to replace assertArrayNear with pytest …
wandadars Oct 2, 2024
021a02a
[test] removed assertArrayNear() function from utilities module
wandadars Oct 2, 2024
a299b87
[test] updated all tests to replace assertArrayNear() with pytest app…
wandadars Oct 2, 2024
98a1eb6
reverted load_yaml to a function and used test_data_path and cantera_…
wandadars Oct 7, 2024
f905236
[test] moved to test_data_path fixture where possible and changed sma…
wandadars Oct 8, 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
108 changes: 108 additions & 0 deletions test/python/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
import cantera
from os import environ
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_configure(config):
config.addinivalue_line("markers", "slow_test: mark test as slow")

def pytest_collection_modifyitems(config, items):
if 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="session", 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
Comment on lines +50 to +63
Copy link
Member

Choose a reason for hiding this comment

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

I may be wrong about what's idiomatic for pytest, but it seems like the simplest thing to do would be to execute these functions directly at the module level, as done in the old utilities.py. The use case for the session-level fixture seems like it would be if it created a singleton object of some sort to actually be used in other some other tests, but here you just want the side-effects that affect the global module state.

This should fix the CI failures. I think the reason you don't see them locally is if you're running the tests through SCons, it's already adding test/data to the cantera data path. The CI runs pytest directly, so has to rely on this for the path update.



@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 +84 to +94
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