Optimized test cleanup logic