-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
base: main
Are you sure you want to change the base?
Pytest transition #1793
Changes from all commits
5ff8d7f
368bf44
0c28847
d250263
feee3a1
501bde4
85166f2
df01b40
5d543d9
4d28f88
51b22e4
c16a19b
867d938
6ad7717
ee74cc1
8735f28
f0b6260
757ba2f
2033a48
2be2883
547de2d
284c144
b9a5597
121425b
32cbb56
ad98b8e
a873457
2833938
348aace
5b83f96
7434e33
ff82d13
7811d79
0dfa4b2
371616c
93180ad
9f8d301
d5b4466
404ffba
0011c52
a55f0d8
e72030a
acbe2eb
6883ff7
2b36ee5
7758522
ef2cb46
d1163d7
da15ca7
e0bd55b
47a4790
b821a5f
873d029
06b3ca7
195ecc8
8313a32
77f19d1
bcaf360
fe3dc00
d442ecf
23efdf1
d02bba9
9b06dad
6196615
715d17f
5b647c0
590cdf1
97a1df7
7ed666b
f4dff24
ee3ddc0
a2e8aac
e45d53e
ea2ff98
6045a03
ed2206a
4a3ba02
a46512f
021a02a
a299b87
98a1eb6
f905236
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 |
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 |
There was a problem hiding this comment.
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.