A pytest plugin that helps you on using a temporary filesystem for testing.
Project description
pytest-tmpfs
A pytest plugin that helps you on using a temporary filesystem for testing
import json
def readjson(path: str) -> dict:
"""
Example:
>>> path = fs.write('a/b/c.json', '{"a": 2}')
>>> readjson(path)
{'a': 2}
"""
with open(path, 'r') as f:
return json.load(f)
This plugin is a fake filesystem manager implementation that relies on the pytest
's tmp_path
fixture to enable safe testing of projects that require disk operations and are difficult to mock.
Alternatives
- pyfakefs: good alternative if you wish to mock filesystem operations.
- tempfile: creates files and directories temporarily.
Install
For installing this package, just type the command below in your terminal:
pip install pytest-tmpfs
Usage
As a fixture
You can use the TmpFs
as a fixture by passing tmpfs
as an argument of your test function.
import pytest_tmpfs
def test_readjson(tmpfs: pytest_tmpfs.TmpFs):
path = tmpfs.write('a/b/d.json', '{"a": 2}')
assert readjson(path) == {'a': 2}
In doctests
By installing this plugin, all doctests are automatically enabled to use fs
, a variable that stores an instance of TmpFs
.
def fscheck(path: str) -> dict:
"""
Example:
>>> fs.touch('a/b/c.txt')
>>> fs.ls('a/b')
['c.txt']
"""
pass
Your own instance
If you seek to use this plugin without pointing to tmp_path
it is possible to create your own instance and to manipulate it the way you want.
import pytest_tmpfs
def test_random():
mypath = pathlib.Path('a/b/c')
fs = TmpFs(mypath)
Features
The following table contains all methods that TmpFs
implements and its descriptions:
Method | Description | Example | Parameters | Return |
---|---|---|---|---|
write | writes a file in the disk and adds line by line content to it | fs.write('a/b.txt', 'Hello', 'World!') | path, *lines | abs path to the file |
read | reads the contents of a file and returns it as a string | fs.read('a/b.txt') | path | abs path to the file |
ls | lists all files inside a folder | fs.ls('a') | path | sorted list of folders and files |
rm | removes a file or a folder | fs.rm('a') | path | True if succeded, False if not |
mkdir | creates one or more folders | fs.mkdir('b/c') | path | abs path to the folder |
mv | moves a file or a folder to another path | fs.mv('a/b.txt', 'b/c') | path, path | abs path to destination |
clean | removes all files inside the root of the tmp fs | fs.clean() | ||
path | converts a relative path into an abs path | fs.path('a') | path | abs path to the file or folder |
tree | prints a tree-like structure of a folder | fs.tree('a') | path | |
tree_format | same as tree, but outputs a string | fs.tree_format('a') | path | tree representation |
cat | prints the content of a file | fs.cat('b/c/b.txt') | path | |
touch | creates an empty file | fs.touch('a/c.txt') | path | abs path to the file |
tmp_cwd | changes CWD to the temporary filesystem folder | fs.tmp_cwd() | abs path to the fs root | |
cwd | returns to the original CWD | fs.cwd() | abs path to the old cwd |
Note:
tmp_cwd
andcwd
can be used withwith statements
.
Contributing
Feel free to contribute to this project, just remember to use pre-commit and to write unit tests for new features/bugs.
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 Distributions
Built Distribution
Hashes for pytest_tmpfs-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 916c2a79d2f2d62706491b66b0ed220d17205abfe168fdbec571cf69c30946ca |
|
MD5 | 8ad42450ece1078977054525daf6c259 |
|
BLAKE2b-256 | 29fb9d53867e1c968d169a09f2b5e6f37bd6f0a403e970193dffb95d753bf059 |