Skip to main content

A pytest plugin that helps you on using a temporary filesystem for testing.

Project description


GitHub Workflow Status codecov

A pytest plugin that helps you on using a temporary filesystem for testing

import json

def readjson(path: str) -> dict:
    >>> 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.


  • pyfakefs: good alternative if you wish to mock filesystem operations.
  • tempfile: creates files and directories temporarily.


For installing this package, just type the command below in your terminal:

pip install pytest-tmpfs


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:
    >>> fs.touch('a/b/c.txt')

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)


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'a/b.txt') path abs path to the file
ls lists all files inside a folder'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'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'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 and cwd can be used with with statements.


Feel free to contribute to this project, just remember to use pre-commit and to write unit tests for new features/bugs.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pytest_tmpfs-0.1.4-py3-none-any.whl (7.7 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page