kedro
kedro copied to clipboard
Iteratively open and close different Kedro sessions from code
Description
From Kedro, I would like to iterate over multiple Kedro sessions and their contexts to test. I am able to iterate from CLI through the different projects but in via pytest the second create session just returns the first. I understand there may be magic for notebooks with %kedro_reload.
We have different Kedro sessions/contexts because we only load the parts of the data catalog and parameters that we need for the pipeline we are running (patterns in settings.py), which eases integration with Kubeflow. Then we want to test that there are no undefined node inputs/outputs and that all catalog entries are being used, project by project.
Steps to Reproduce
Below is an example of the fixture and test. I'll save space and not copy the second fixture and test (session2 & test_project_catalog2) that have example_project2 and example_pipeline2:
@pytest.fixture
def session1():
os.environ["PROJECT"] = "example_project1"
os.environ["PIPELINE"] = "example_pipeline1"
bootstrap_project(Path.cwd())
try:
return KedroSession.create(
package_name="example_package",
project_path=Path(Path.cwd()),
save_on_close=False,
)
except Exception as exc:
raise KedroCliError(
f"Unable to instantiate Kedro session.\nError: {exc}"
) from exc
def test_project_catalog(session1):
context = session1.load_context()
from settings.py:
CONFIG_LOADER_ARGS = {
"config_patterns": {
"catalog": [f"catalogs/resolved/kf_catalog_{project}_{pipeline}.yml"],
"parameters": [
f"parameters/set_parameters/parameters_{project}_{pipeline}.yml",
f"parameters/{project}/*.yml",
],
},
Expected Result
session1 and session2 should be different. session2 is correct if I remove session1 or call it first.
Actual Result
session2 is just session1 again.
Your Environment
(Kedro version: 0.18.10)