Package: kedro-pytest
kedro-pytestHelps you testingKedroplugins withpytest.fromkedro_pytestimportTestKedrodeftest_count_cli(tkedro:TestKedro):tkedro.new('project')# ^ creates a new project in a temporary directorytkedro.create_pipeline('pipe')# ^ ceates a pipeline and two csv entries in catalog by defaulttkedro.update_yml('conf/base/catalog.yml',{'new_dset':{'type':'pandas.JSONDataSet','filepath':'a.json'}})# ^ adds a new dataset to the catalogresult=tkedro.cli(['count','catalog'],['--type','pandas.CSVDataSet'])# ^ returns a click.testing.Result objectassertint(result.output)==2tkedro.stop()# ^ cleans the temporary directory (only required for doctests)deftest_helloworld_hook(tkedro:TestKedro):withtkedro.new('project')astk:tk.create_pipeline('pipe')assert'Hello world!'intk.run('pipe').outputThis plugin focuses on generating minimalKedrostructures, such as small
pipelines, and a small project, in order to simplify testing ofKedroplugins
in a realKedroenvironment. Also, to make it safe for testing, the plugin
relies onpytest-tmpfsto create a temporary filesystem for the project.InstallFor installing this package, just type the code below in your terminal:pip install kedro-pytestUsageAs a fixtureYou can use theKedroPytestas a fixture by passing tkedro as an argument of your test function.fromkedro_pytestimportTestKedrodeftest_fixture(tkedro:TestKedro):...In doctestsYou can always use fixtures in doctests. To do so, you have to inject it into
the doctest namespace in theconftestfile.@pytest.fixture(autouse=True)defadd_to_doctests(doctest_namespace:dict,tkedro:TestKedro):"""Adds the tkedro fixture to the doctest namespace."""doctest_namespace["kedro"]=tkedroconftest.pydeffunction():"""Example:>>> kedro.new('project')>>> kedro.create_pipeline('pipe')>>> kedro.stop()"""...code.pyYour own instanceIf you seek to use this plugin without pointing totmp_pathit is possible to create your own instance and manipulate it the way you want.fromkedro_pytestimportTestKedrofrompathlibimportPathdeftest_my_path():tkedro=TestKedro(path=Path.cwd())...MethodsThe following table contains all methods thatTestKedroimplements and its descriptions:MethodDescriptionParametersReturnnewCreates a new project in a temporary directory.name: strTestKedrocreate_pipelineCreates a minimal dummy pipeline with two csv entries in catalog by default and a parameter.name: str, *content: str~write_ymlWrites a yaml file in the project.path: str, content: dictstr: abs path to the fileread_ymlReads a yaml file in the project.path: strdict: content of the fileupdate_ymlUpdates a yaml file in the project. Can be nested, it will replace recursively.path: str, content: dictstr: abs path to the filecliRuns a kedro cli command in the project.command: list[str], args: list[str]click.testing.ResultrunRuns a pipeline in the project.pipeline: str, run_command: list[str]click.testing.ResultstopCleans the temporary directory.~~NoteThe create_pipeline method differs from the cli command because it creates a minimal pipeline (a pipeline.py with a dummy implementation, a catalog.yml, and a
parameters.yml), and it automatically adds the pipeline to the registry, while the cli create_pipeline would create a folder containing a clean pipeline skeleton and
will not add it to the registry.Warningcwd switches between temporary and the last cwd when using new and stop methods of TestKedro. A new instance of the fixture is created for each test, so you don't
have to worry about it in tests, but in doctests it may be useful. Also, the use of stop can be avoided by using with statements.Advanced usageIf the methods mentioned above are not enough for your needs, you can always use thefsattribute of theTestKedroinstance to manipulate the filesystem.importjsondeftest_advanced(tkedro:TestKedro):tkedro.new('project')tkedro.cli(['json','catalog'])text=tkedro.fs.read_text('conf/base/catalog.json')...ContributingFeel free to contribute to this project, just remember to install pre-commit, and to write unit tests for it.