Dynamic fixture creation for your tests
Project description
python-fixturify-project
Dynamic fixture creation for your tests
A Python port of node-fixturify-project
Installation
pip install -U python-fixturify-project
or install with Poetry
poetry add python-fixturify-project --dev
Usage
python-fixturify-project
is 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.
from python_fixturify_project import Project
dir_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 structure
project = Project(files=dir_json)
# add new files to the project, merging with the existing directory structure
p.write({
"new_file.txt": "some text"
})
# read the actual contents on disc
actual_dir_json = p.read()
Ignore Files
By default, the read()
function will ignore all hidden files and directories in your Project file structure. This can be overridden by using the ignore_patterns
function parameter, which
takes a list of glob pattern strings. This may be slightly confusing, as glob patterns are normally used in an inclusive manner when performing file-system searches, however any patterns
provided to the ignore_patterns
parameter will be used in an exclusive manner. 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",
}
project = Project(files=files)
dir_json = project.read(ignore_patterns=["**/.git", "**/.git/**", "**/ignore_me"]) # Default is ["**/.git", "**/.git/**"]
assert dir_json == {
'.github': {
'do_not_ignore_me': {
'a_file': 'some text',
},
},
'do_not_ignore_me': 'some text',
}
Usage when writing tests
python-fixutrify-project
becomes even more useful when combining it with something like syrupy
.
from python_fixturify_project import Project
def test_mutating_project(snapshot):
project = Project(files=INITIAL_DIR_JSON)
mutate_files_for_some_reason(p.base_dir)
# ensure mutations were as expected
assert project.read() == snapshot
Or you can use the project.get
method to get the path to a file in the project.
from python_fixturify_project import Project
def test_mutating_project(snapshot):
project = Project(files=INITIAL_DIR_JSON)
mutate_files_for_some_reason(p.base_dir)
# ensure mutations were as for single file
assert project.get('path/to/a/file.py') == snapshot(name='path/to/a/file.py')
🛡 License
This project is licensed under the terms of the MIT
license. See LICENSE for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python-fixturify-project-0.1.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d56444a8e26be6f0e49b19f7284bba202ee1633090c80bc4d2c9fba8a00e742e |
|
MD5 | 31c0ac69f650df5d426017ab3e4a65ef |
|
BLAKE2b-256 | 14249c8378fc922fa121e680b18d88bab4e571203c8a10fdf1b06493940441da |
Hashes for python_fixturify_project-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c656fb1b3222da4a78627a5a3c6a04ff223dee29b1d1f85b3d55e6f9cb8e77e |
|
MD5 | 6c285e95f6570d536497b4d0d3bc2d7f |
|
BLAKE2b-256 | 8f4807a291b1f3772f949fcf1f076ea64076ebe8ab735d34738f3b842ff87432 |