Skip to main content

Provides path for uniform access to test resources in isolated directory

Project description

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.

Project details


Download files

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

Source Distribution

pytest_resource_path-1.4.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_resource_path-1.4.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_resource_path-1.4.1.tar.gz.

File metadata

  • Download URL: pytest_resource_path-1.4.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_resource_path-1.4.1.tar.gz
Algorithm Hash digest
SHA256 ee17e25e059ef622c376a85a0ae2cfbe32a4e0a267c7266dde78bab4c0a05e90
MD5 e728a960f1152f1cf4d1d4226ca194bb
BLAKE2b-256 7204d4e333aa83e338dd4d94714e98f094f44a49f2ea519674f9dd6b981454eb

See more details on using hashes here.

File details

Details for the file pytest_resource_path-1.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_resource_path-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a6ac94b3aac839d4cb850e0891100b0d28811236ebb7d9e9b98cda15098542e1
MD5 a97456b4ca253e0c510c278b0a4ee518
BLAKE2b-256 f783e4b248ce4ba40d5f703f736bb308cb9b200137ecdcdf2fcb46c9d28da8ae

See more details on using hashes here.

Supported by

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