Skip to content

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
  • AsyncTestCase base class with AnyIO-backed cleanup
  • AsyncMockManager for managing async mocks with automatic cleanup
  • async_mock_context for async context manager mocking

2. Updated conftest.py

  • Added async_mock_manager fixture 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.py for synchronous tests
  • Created test_native_execution_async.py for 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

  1. No AsyncMock in synchronous test methods - Use regular functions that return coroutines
  2. Proper event loop management - Let AnyIO's pytest plugin handle loops for async tests
  3. Avoid gc.collect() - Let Python handle garbage collection naturally
  4. Use AnyIO patterns - Prefer AnyIO helpers over direct event loop access
  5. Test isolation - Each test gets its own event loop and proper cleanup

Future Considerations

  1. Use the AsyncTestCase or async_mock_manager for any new async tests
  2. Always mark async test methods with @pytest.mark.anyio
  3. Mock async methods by creating functions that return coroutines
  4. Avoid mixing sync and async code in the same test method
  5. Use AnyIO helpers to avoid event loop coupling