Package: python-fixturify-project
python-fixturify-projectDynamic fixture creation for your testsA Python port ofnode-fixturify-projectInstallationpipinstall-Upython-fixturify-projector install withPoetrypoetryaddpython-fixturify-project--devUsagepython-fixturify-projectis a Python package that provides a way to create dynamic fixtures for your tests. Fixtures are real directories and files, written to a temporary directory.frompython_fixturify_projectimportProjectdir_json={"valid_file.txt":"some text","nested_dir":{"valid_empty_file.txt":"","another_nested_empty_dir":{},"another_nested_dir":{"last_nested_empty_dir":{},"final_text_file.txt":"some text",},},}# create a new project with the given directory structureproject=Project(files=dir_json)# add new files to the project, merging with the existing directory structurep.write({"new_file.txt":"some text"})# read the actual contents on discactual_dir_json=p.read()Ignore FilesBy default, theread()function will ignore all hidden files and directories in your Project file structure. This can be overridden by using theignore_patternsconstructor argument, which
takes a list of glob pattern strings. Any patterns provided to theignore_patternsargument will be used in anexclusivemanner. For example:files={".git":{"a_nested_dir":{}},".github":{"ignore_me":{},"do_not_ignore_me":{"a_file":"some text"}},"ignore_me":"some text","do_not_ignore_me":"some text",}# Default ignore_patterns is ["**/.git", "**/.git/**"]project=Project(ignore_patterns=["**/.git","**/.git/**","**/ignore_me"])project.write(files)assertproject.read()=={'.github':{'do_not_ignore_me':{'a_file':'some text',},},'do_not_ignore_me':'some text',}Recommended Usage Patternspython-fixutrify-projectbecomes even more useful when combining it with tools likepytestand something likesyrupy, which usesjest-like snapshots for testing. The example below combinespython-fixturify-projectwithpytest's fixtures, andsyrupyto create a snapshot test.First, we define a fixture to setup and teardown ourProjectinstance:# conftest.pyimportpytestfrompython_fixturify_projectimportProject@pytest.fixturedefproject():project=Project()yieldprojectproject.dispose()This fixture usespytest'syieldfixture pattern, which allows us to run some code after the test has completed. In this case, we use thedispose()method to remove the temporary directory created bypython-fixturify-project.frompython_fixturify_projectimportProjectdeftest_mutating_project(project,snapshot):project.write({"a_file.txt":"some text","a_dir":{"another_file.txt":"some text",},"path":{"to":{"a_file.py":"# some python code",},},})mutate_files_for_some_reason(p.base_dir)# ensure mutations were as expectedassertproject.files==snapshotOr you can use theproject.getmethod to get the path to a file in the project.frompython_fixturify_projectimportProjectdeftest_mutating_project(snapshot):project.write({"a_file.txt":"some text","a_dir":{"another_file.txt":"some text",},"path":{"to":{"a_file.py":"# some python code",},},})mutate_files_for_some_reason(p.base_dir)# ensure mutations were as  for single fileassertproject.get('path/to/a_file.py')==snapshot(name='path/to/a_file.py')Skip Dispose (for debugging)If you want to skip thedispose()call, you can set theFIXTURIFY_SKIP_DISPOSEenvironment variable to1.FIXTURIFY_SKIP_DISPOSE=1pytestThis can be useful if you want to inspect the contents of the temporary directory after the test has completed.🛡 LicenseThis project is licensed under the terms of theMITlicense. SeeLICENSEfor more details.