-
Notifications
You must be signed in to change notification settings - Fork 4
Add test cleanup fixtures and remove global state #42
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
Open
bjarketol
wants to merge
12
commits into
EUFLOW:main
Choose a base branch
from
bjarketol:fix-tests-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3ab28ff
updating foxes tests
SchmJo dc2da37
Add test cleanup fixtures and remove global state
bjarketol e78313c
black
bjarketol 1fa81e1
handle foxes versions
bjarketol e1d9b02
foxes versions
bjarketol 5e069dc
foxes versions
bjarketol 6f91354
Fix tests
bjarketol 2906526
use default foxes engine
bjarketol d535903
Simplify foxes engine handling
bjarketol 9c0376e
Clean up test files
bjarketol dad1787
Merge branch 'main' into fix-tests-cleanup
bjarketol 77bdcab
Fix pre-commit issues
bjarketol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """ | ||
| Pytest configuration and fixtures for WIFA tests. | ||
|
|
||
| Provides: | ||
| - Pre-test cleanup of leftover output directories | ||
| - Output directory fixtures with conditional cleanup (preserved on failure) | ||
| """ | ||
|
|
||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| # Store test outcomes for conditional cleanup | ||
| _test_outcomes = {} | ||
|
|
||
|
|
||
| @pytest.hookimpl(hookwrapper=True) | ||
| def pytest_runtest_makereport(item, call): | ||
| """Track test outcomes to conditionally preserve output on failure.""" | ||
| outcome = yield | ||
| report = outcome.get_result() | ||
| if report.when == "call": | ||
| _test_outcomes[item.nodeid] = report.failed | ||
|
|
||
|
|
||
| def pytest_configure(config): | ||
| """Register custom markers.""" | ||
| config.addinivalue_line( | ||
| "markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def cleanup_old_outputs(): | ||
| """Remove output directories from previous test runs at session start.""" | ||
| patterns = [ | ||
| "output_pywake_*", | ||
| "output_test_*", | ||
| "output", | ||
| ] | ||
| for pattern in patterns: | ||
| for path in Path(".").glob(pattern): | ||
| if path.is_dir(): | ||
| shutil.rmtree(path) | ||
| elif path.is_file(): | ||
| path.unlink() | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def output_dir(request, tmp_path): | ||
| """ | ||
| Provide a unique temporary output directory for tests. | ||
|
|
||
| Cleans up automatically on test success, preserves on failure for debugging. | ||
| """ | ||
| yield tmp_path | ||
|
|
||
| # Only clean up if test passed | ||
| test_failed = _test_outcomes.get(request.node.nodeid, False) | ||
| if not test_failed: | ||
| if tmp_path.exists(): | ||
| shutil.rmtree(tmp_path) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def named_output_dir(request): | ||
| """ | ||
| Provide a named output directory based on test name. | ||
|
|
||
| Use this when tests need output in the current working directory | ||
| (e.g., for Code Saturne integration). | ||
|
|
||
| Cleans up automatically on test success, preserves on failure. | ||
| """ | ||
| test_name = request.node.name.replace("[", "_").replace("]", "_").rstrip("_") | ||
| output_path = Path(f"output_test_{test_name}") | ||
| output_path.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| yield output_path | ||
|
|
||
| # Only clean up if test passed | ||
| test_failed = _test_outcomes.get(request.node.nodeid, False) | ||
| if not test_failed: | ||
| if output_path.exists(): | ||
| shutil.rmtree(output_path) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cleanup_output_dir(request): | ||
| """ | ||
| Cleanup fixture for tests that write to the default 'output/' directory. | ||
|
|
||
| Use this when tests can't control their output location (e.g., when YAML | ||
| specifies output_folder). Cleans up on success, preserves on failure. | ||
| """ | ||
| output_path = Path("output") | ||
|
|
||
| yield output_path | ||
|
|
||
| # Only clean up if test passed | ||
| test_failed = _test_outcomes.get(request.node.nodeid, False) | ||
| if not test_failed: | ||
| if output_path.exists(): | ||
| shutil.rmtree(output_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.