A py.test plugin providing temporary directories in unit tests.
Project description
Testing code by invoking executable which potentially creates and deletes files and directories can be hard and error prone.
The purpose of this module is to simplify this task.
pytest-testdirectory provides a py.test fixture for working with temporary directories.
Installation
To install pytest-testdirectory:
pip install pytest-testdirectory
Usage
To make it easy to use in with py.test the TestDirectory object can be injected into a test function by using the testdirectory fixture.
Example:
def test_this_function(testdirectory): images = testdirectory.mkdir('images') images.copy_files('test/images/*') r = testdirectory.run('imagecompress --path=images') # r is an RunResult object containing information about the command # we just executed assert r.stdout.match('*finished successfully*')
The testdirectory is an instance of TestDirectory and represents an actual temporary directory somewhere on the machine running the test code. Using the API we can create additional temporary directories, populate them with an initial set of files and finally run some executable and observe its behavior.
Relase new version
Edit NEWS.rst and wscript (set correct VERSION)
Run
./waf upload
Source code
The main functionality is found in src/testdirectory.py and the corresponding unit test is in test/test_testdirectory.py if you want to play/modify/fix the code this would, in most cases, be the place to start.
Developer Notes
We try to make our projects as independent as possible of a local system setup. For example with our native code (C/C++) we compile as much as possible from source, since this makes us independent of what is currently installed (libraries etc.) on a specific machine.
To “fetch” sources we use Waf (https://waf.io/) augmented with dependency resolution capabilities: https://github.com/steinwurf/waf
The goal is to enable a work-flow where running:
./waf configure ./waf build --run_tests
Configures, builds and runs any available tests for a given project, such that you as a developer can start hacking at the code.
For Python project this is a bit unconventional, but we think it works well.
Tests
The tests will run automatically by passing --run_tests to waf:
./waf --run_tests
This follows what seems to be “best practice” advise, namely to install the package in editable mode in a virtualenv.
Notes
Why use an src folder (https://hynek.me/articles/testing-packaging/). tl;dr you should run your tests in the same environment as your users would run your code. So by placing the source files in a non-importable folder you avoid accidentally having access to resources not added to the Python package your users will install…
Python packaging guide: https://packaging.python.org/distributing/
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
Built Distribution
File details
Details for the file pytest_testdirectory-4.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: pytest_testdirectory-4.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 305d2fb074ecad955009e8ee821533c0811c1536cf33ec5916a954609ddbe9ac |
|
MD5 | f4b52151b7e8321ce5e58188c0ee6273 |
|
BLAKE2b-256 | 15de2d70db1f474e05fb5b44be60be1d3d9850155874ddfa8c7e5a8eef0ec369 |