Async Test Architecture Implementation Summary
Overview
Successfully implemented an AnyIO-based async test architecture to eliminate RuntimeWarning messages about unawaited coroutines in the nf.core test suite.
Changes Made
1. Created async_test_utils.py
- Provides utilities for proper async test management
AsyncTestCasebase class with AnyIO-backed cleanupAsyncMockManagerfor managing async mocks with automatic cleanupasync_mock_contextfor async context manager mocking
2. Updated conftest.py
- Added
async_mock_managerfixture for managed async mocks - Added backend fixtures for AnyIO's bundled pytest plugin
- Imports async test utilities
3. Split test_native_execution_integration.py
- Created
test_native_execution_sync.pyfor synchronous tests - Created
test_native_execution_async.pyfor tests involving async code - Properly mocked async methods using coroutine functions instead of AsyncMock
4. Fixed test_parallel_synthesis.py
- Removed
gc.collect()call that was triggering warnings - Used weak references for cleanup tracking instead
- Let Python's garbage collector handle cleanup naturally
5. Updated async utilities
- Standardized on AnyIO helpers for sync/async boundaries
Results
- ✅ All 718 tests pass
- ✅ 0 warnings (previously had 3 RuntimeWarnings)
- ✅ No deprecation warnings
- ✅ Cleaner, more maintainable async test code
- ✅ Better test isolation and reliability
Key Principles Applied
- No AsyncMock in synchronous test methods - Use regular functions that return coroutines
- Proper event loop management - Let AnyIO's pytest plugin handle loops for async tests
- Avoid gc.collect() - Let Python handle garbage collection naturally
- Use AnyIO patterns - Prefer AnyIO helpers over direct event loop access
- Test isolation - Each test gets its own event loop and proper cleanup
Future Considerations
- Use the
AsyncTestCaseorasync_mock_managerfor any new async tests - Always mark async test methods with
@pytest.mark.anyio - Mock async methods by creating functions that return coroutines
- Avoid mixing sync and async code in the same test method
- Use AnyIO helpers to avoid event loop coupling