Skip to content

Add test cleanup fixtures and remove global state#42

Open
bjarketol wants to merge 9 commits intoEUFLOW:mainfrom
bjarketol:fix-tests-cleanup
Open

Add test cleanup fixtures and remove global state#42
bjarketol wants to merge 9 commits intoEUFLOW:mainfrom
bjarketol:fix-tests-cleanup

Conversation

@bjarketol
Copy link
Contributor

@bjarketol bjarketol commented Jan 20, 2026

Summary

Closes #41

  • Add pytest fixtures for automatic test cleanup
  • Mark slow tests (wayve) with @pytest.mark.slow

Problem

Tests were leaving behind output directories (output_pywake_4wts/, output_test_foxes/, etc.) that accumulated over multiple runs.

Solution

Created tests/conftest.py with:

  • Pre-cleanup fixture - Removes leftover output directories at session start
  • output_dir fixture - Provides unique temp directory per test, cleans up on success, preserves on failure
  • Failure tracking - Uses pytest hooks to detect failures and conditionally preserve output for debugging

Changes

File Change
tests/conftest.py New file with cleanup fixtures
tests/test_foxes.py Use fixtures
tests/test_pywake.py Use output_dir fixture
tests/test_cs.py Use output_dir fixture
tests/test_wayve.py Use output_dir fixture, add slow marker

Test plan

  • Run uv run python -m pytest tests/ -m "not slow" - all 33 tests pass
  • Verify no output_* directories remain after test run

SchmJo and others added 6 commits January 20, 2026 13:31
- Create tests/conftest.py with pytest fixtures for automatic cleanup
- Add output_dir fixture using tmp_path with conditional cleanup
  (preserves output on test failure for debugging)
- Add foxes_engine fixture to replace global engine state
- Add session-scoped pre-cleanup to remove leftover output directories
- Register 'slow' marker for long-running tests

- Update test_foxes.py to use fixtures instead of global engine
- Update test_pywake.py to use output_dir fixture
- Update test_cs.py to use output_dir fixture
- Update test_wayve.py to use output_dir fixture and mark as slow

Tests now properly clean up after themselves while preserving
output on failure for debugging purposes.
Copy link
Contributor

Choose a reason for hiding this comment

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

engine.initialize() was actually removed in foxes v1.7.0

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just leave out the mentioning of the engine alltogether, then it should always fall back to using a default (also for older versions)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I changed it so it only resets state.

@bjarketol
Copy link
Contributor Author

@SchmJo can you review this? or what is the process?

@SchmJo
Copy link
Contributor

SchmJo commented Jan 22, 2026

I am not entirely sure why you are introducing conftest.py, which looks like a complication. Was the testing not running fine originally? Usually tests run as part of CI/CD , ie., github actions, and the leftover folders don't matter. That being said, I am of course happy with cleaning up after tests.

I also don't understand your special treatment of foxes engines, they should not matter for tests. My feeling is that I am missing something. Also, I do not feel comfortable reviewing the pywake part, I guess you need to catch Julian for this. The foxes part I can do, once I understand the main idea!

@bjarketol
Copy link
Contributor Author

@SchmJo The main goals are:

  • Avoid many folders on testing by letting tests perform a clean-up after tests pass
  • Mark very slow tests, to optionally skip those for faster iteration cycles

I am happy to skip the foxes engine resetting, that was a minor thing.

 * Ensure run_pywake correctly uses output_dir argument
@bjarketol
Copy link
Contributor Author

@SchmJo I removed the foxes engine resetting and the engine argument to the tests.

Can you see if you are happy with the rest?

@bjarketol
Copy link
Contributor Author

bjarketol commented Jan 22, 2026

@kilojoules can you review the pywake part?

Copy link
Contributor

@SchmJo SchmJo left a comment

Choose a reason for hiding this comment

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

Thanks for your work on WIFA, I am still not sure why you are so interested in the tests - are they relevant for your own workflow?


import pytest

# Handle different foxes versions - reset_engine location varies
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove the engine related parts?

Provides:
- Pre-test cleanup of leftover output directories
- Output directory fixtures with conditional cleanup (preserved on failure)
- FOXES engine fixture with proper initialization and teardown
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't need the engine part

print("SETTING ENGINE:", engine)

for yaml_input in wes_dir.glob("system.yaml"):
if "_noXYgrid" not in str(yaml_input):
Copy link
Contributor

Choose a reason for hiding this comment

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

This "_noXYgrid" part is deprecated, this "if" clause can be removed

reset_engine()
engine = None
raise e
run_foxes(yaml_input, output_dir=output_dir, engine=None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Completely remove "engine=None" part

test_foxes_simple_wind_rose()
import pytest

pytest.main([__file__, "-v"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need these two last lines? Usually the tests are run via "pytest tests" or similar, see github workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests do not clean up after themselves

2 participants