Skip to main content

pytest-resource-path

Test CodeQL Code Coverage Maintainability Dependabot Python versions PyPI - Downloads X URL

Provides path for uniform access to test resources in isolated directory

Features

Let's isolate test resources from test code

The test which use test resource is not so many. If we place test resource with test code, these resources will fill the display area of explorer. Wouldn't it be more productive if the files or directories which is not related with almost tests wasn't usually displayed?

pytest-resource-path provides path for uniform access to test resources in isolated directory.

This pytest plugin assumes that test resource is placed under testresources directory directly under tests. (Don't worry, you can customize these directory names by pytest.ini)

tests/
+-- some_test_package/
|   +-- some_test_module.py
+-- testresources/
    +-- some_test_package/
        +-- some_test_module/

Installation

You can install "pytest-resource-path" via pip from PyPI:

pip install pytest-resource-path

Usage

Basic

You can use fixture resource_path which is pathlib.Path instance (absolte path).

def test_method(resource_path):
    text_test_resource = (resource_path / 'test_resource.txt').read_text()

When assume that above test_method is in tests/some_tests_package_some_test_module.py, you have to place test_resource.txt following directory:

tests/
+-- some_test_package/
|   +-- some_test_module.py
+-- testresources/
    +-- some_test_package/
        +-- some_test_module/
            +-- test_method/
                +-- test_resource.txt

If you want to omit directory per method, you can do:

def test_method(resource_path):
    text_test_resource = Path(f'{resource_path}.txt').read_text()

Note that the class name is not used in the path since It felt redundant in design.

Get path to test resources root directory

You can use fixture resource_path_root which is pathlib.Path instance (absolte path) pointing to testresources.

def test_method(resource_path_root):
    text_test_resource = (resource_path_root / 'test_resource.txt').read_text()
tests/
+-- some_test_package/
|   +-- some_test_module.py
+-- testresources/
    +-- test_resource.txt

This fixture may be your help duaring migration period of directory structure. Or, may be usiful to preapare common directory with some of tests.

def test_method(resource_path_root):
    text_test_resource = (resource_path_root / 'common/test_resource.txt').read_text()
tests/
+-- some_test_package/
|   +-- some_test_module.py
+-- testresources/
    +-- common/
        +-- test_resource.txt

How to customize directory names

To traverse directory structure, this plugin requires to fix directory names.

By default:

directory requires to be named
Root directory of tests tests
Root directory of test resources testresources

You can customize these required names by pytest.ini

Ex:

[pytest]
resource-path.directory-name-tests = integrationtests
resource-path.directory-name-test-resources = data

Above customize fits following directory strucure:

integrationtests/
+-- some_test_package/
|   +-- some_test_module.py
+-- data/
    +-- some_test_package/
        +-- some_test_module/

License

Distributed under the terms of the MIT license, "pytest-resource-path" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Contributing

Contributions are very welcome. Please ensure the coverage at least stays the same before you submit a pull request.

Credits

This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Release files for pytest-resource-path 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-resource-path 1.5.0
File Size Uploaded
pytest_resource_path-1.5.0.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-resource-path 1.5.0
File Interpreter ABI Platform
pytest_resource_path-1.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / pytest_resource_path-1.5.0.tar.gz

Download URL pytest_resource_path-1.5.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f56a37370a0f01cd9a5794d361c24e7aa1c033a69260870d9f46b263624527c9
BLAKE2b-256 checksum
How to use checksums
1206d3a7713221cb29fb49c1d003d7690d686a59820abebd27bd76a5d1e63a02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / pytest_resource_path-1.5.0-py3-none-any.whl

Download URL pytest_resource_path-1.5.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f9f3a9a4b6a1fbc89cffb466b4695d43d2244b3f39aefc135187466c333d953
BLAKE2b-256 checksum
How to use checksums
daa6663739a752f6bb012a7cd9277d96c811acd02b81255dab514910e859c82d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page