[Test] Improve test reuse in test/test_foreach.py for out-of-tree bac…