pytest_tmp_files is a pytest plugin that provides a fixture for creating temporary file hierarchies. This fixture is called tmp_files, and you can think of it as an extension of the built-in tmp_path fixture. In addition to creating a unique temporary directory for each test, tmp_files also fills in that directory with any files needed for that test.
The files to create are specified by a dictionary provided to fixture via indirect parametrization. For example, here’s a test for a function that searches for files whose contents match a given regular expression:
import pytest, re
from pathlib import Path
def find_text(top, pattern):
hits = set()
for path in Path(top).glob('**/*'):
if path.is_file() and re.search(pattern, path.read_text()):
hits.add(path)
return hits
@pytest.mark.parametrize(
'tmp_files, pattern, expected', [
({'a': 'x'}, 'x', {'a'}),
({'a': 'x'}, 'y', set()),
({'a/b': 'x'}, 'x', {'a/b'}),
({'a/b': 'x'}, 'y', set()),
({'a/b': 'x', 'c': 'y'}, 'x', {'a/b'}),
({'a/b': 'x', 'c': 'y'}, 'y', {'c'}),
({'a/b': 'x', 'c': 'y'}, '[xy]', {'a/b', 'c'}),
],
indirect=['tmp_files'],
)
def test_find_text(tmp_files, pattern, expected):
expected = {
tmp_files / p
for p in expected
}
assert find_text(tmp_files, pattern) == expected
The first parameter in each set (the dictionary) specifies the files to create. The keys are file paths and the values are file contents, so {'a/b': 'x'} specifies a subdirectory a containing a text file b with the contents x. Although not shown here, it’s also possible to create different kinds of files (e.g. binary files, symlinks, hard links, named FIFOs) and to specify file metadata (e.g. permissions, modification times).
If this plugin sounds useful to you, you may also be interested in Parametrize From File. These two packages work really well together, because tmp_files lends itself to long, multi-line parameters and Parametrize From File makes such parameters easier to manage.
Check out the documentation for more information.
Metadata
Release files for pytest-tmp-files 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pytest_tmp_files-0.0.2.tar.gz | 5.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_tmp_files-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / pytest_tmp_files-0.0.2.tar.gz
| Download URL | pytest_tmp_files-0.0.2.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b2c98559d4d79c0f44b49d9010d65b2508a68c21312d51d50363f005e718666
|
|
BLAKE2b-256 checksum How to use checksums |
036347b7aca34b6f496eea6525a44e773d72c8bdc5be5a63cc83734c94701267
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.31.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
|
Release files / pytest_tmp_files-0.0.2-py3-none-any.whl
| Download URL | pytest_tmp_files-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
47ff5267063285c5388137c284fc46d26bbbf0331e6b2c68bc9815fe116d5a29
|
|
BLAKE2b-256 checksum How to use checksums |
5a9d572fccbc32e03334c6154cdcd6b0b0679e52ae3961d68074263b4aa2ac51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.31.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
|
Security for pytest-tmp-files 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.