File: pytest_resource_path.py

package info (click to toggle)
python-pytest-resource-path 1.3.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: python: 398; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Test for pytest."""
from pathlib import Path

import pytest  # type: ignore

from pytest_resource_path.path_to_resource_factory import PathToResourceFactory

__all__ = [
    "INI_KEY_DIRECTORY_NAME_TESTS",
    "INI_KEY_DIRECTORY_NAME_TEST_RESOURCES",
    "pytest_addoption",
    "resource_path",
    "resource_path_root",
    "create_path_to_resource_factory",
]

INI_KEY_DIRECTORY_NAME_TESTS = "resource-path.directory-name-tests"
INI_KEY_DIRECTORY_NAME_TEST_RESOURCES = "resource-path.directory-name-test-resources"


def pytest_addoption(parser):
    """Adds options for pytest-resource-path."""
    parser.addini(
        INI_KEY_DIRECTORY_NAME_TESTS,
        "Directory name for tests",
        default=PathToResourceFactory.DIRECTORY_NAME_TESTS_DEAFAULT,
    )
    parser.addini(
        INI_KEY_DIRECTORY_NAME_TEST_RESOURCES,
        "Directory name for test resources",
        default=PathToResourceFactory.DIRECTORY_NAME_TEST_RESOURCES_DEFAULT,
    )


@pytest.fixture
def resource_path(request):
    """Fixture to get path to resource."""
    yield create_path_to_resource_factory(request).create(request.function)


@pytest.fixture(scope="package")
def resource_path_root(request):
    """Fixture to get path to resource root."""
    yield create_path_to_resource_factory(request).create_path_to_resource_root(Path(str(request.fspath)).resolve())


def create_path_to_resource_factory(request):
    """Creates PathToResourceFactory."""
    path_tests = Path(request.config.getini(INI_KEY_DIRECTORY_NAME_TESTS))
    path_test_resources = Path(request.config.getini(INI_KEY_DIRECTORY_NAME_TEST_RESOURCES))
    return PathToResourceFactory(path_tests, path_test_resources)