Coder Social home page Coder Social logo

yukihiko-shinoda / pytest-resource-path Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 82 KB

Pytest plugin to provide path for uniform access to test resources.

License: MIT License

Python 100.00%
python python3 pytest pytest-plugin fixture fixture-loading fixture-loader pytest-fixtures filesystem file-system

pytest-resource-path's Introduction

pytest-resource-path

Test Test Coverage Maintainability Code Climate technical debt Updates Python versions PyPI - Downloads Twitter 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.

pytest-resource-path's People

Contributors

yukihiko-shinoda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pytest-resource-path's Issues

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Only works with function scoped fixture

this does not work apparently:

@pytest.fixture(scope="session")
def test_foo(resource_path_root):
     pass
ScopeMismatch: You tried to access the 'function' scoped fixture 'resource_path_root' with a 'session' scoped request object, involved factories

get_text in documentation should be read_text

Hello - thanks for this package!

I just noticed that a couple of examples in the documentation are like:

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

but they should instead be:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.