Working with static files

Testsuite provides functions to work with static files associated with testscase. It searches for files in static directoy relative to the current file. If your tests is located in tests/test_foo.py testsuite will search for the static file filename.txt in the following order:

Static file lookup order

Name

Example path

Per case

tests/static/test_foo/test_case_name/filename.txt

Per file

tests/static/test_foo/filename.txt

Per package default/

tests/static/default/filename.txt

Static directory

tests/static/filename.txt

Initial data dirs

see initial_data_path()

Not found

FileNotFoundError would be raised

Fixtures

load

testsuite.plugins.common.load()

Returns LoadFixture instance.

class testsuite.plugins.common.LoadFixture[source]

Load file from static directory.

Example:

def test_something(load):
    data = load('filename')
Returns:

LoadFixture callable instance.

__call__(filename: Union[str, Path], encoding='utf-8', errors=None, *, missing_ok=False) Union[bytes, str][source]

Load static text file.

Parameters:
  • filename – static file name part.

  • encoding – stream encoding, see open().

  • errors – error handling mode see open().

Returns:

str instance.

load_binary

testsuite.plugins.common.load_binary()

Returns LoadBinaryFixture instance.

class testsuite.plugins.common.LoadBinaryFixture[source]

Load binary data from static directory.

Example:

def test_something(load_binary):
    bytes_data = load_binary('data.bin')
__call__(filename: Union[str, Path]) bytes[source]

Load static binary file.

Parameters:

filename" – static file name part

Returns:

bytes file content.

load_json

testsuite.plugins.common.load_json()

Returns LoadJsonFixture instance.

class testsuite.plugins.common.LoadJsonFixture[source]

Load json doc from static directory.

Json loader runs json_util.loads(data, ..., *args, **kwargs) hooks. It does: * bson.json_util.object_hook() * mockserver substitution

Example:

def test_something(load_json):
    json_obj = load_json('filename.json')
__call__(filename: Union[str, Path], *args, missing_ok=False, missing=None, **kwargs) Any[source]

Call self as a function.

load_yaml

testsuite.plugins.common.load_yaml()

Returns LoadYamlFixture instance.

class testsuite.plugins.common.LoadYamlFixture[source]

Load yaml doc from static directory.

def test_something(load_yaml):
    yaml_obj = load_yaml('filename.yaml')
__call__(filename: Union[str, Path], *args, **kwargs) Any[source]

Call self as a function.

open_file

testsuite.plugins.common.open_file()

Returns OpenFileFixture instance.

class testsuite.plugins.common.OpenFileFixture[source]

Open static file by name.

Only read-only open modes are supported.

Example:

def test_foo(open_file):
    with open_file('foo') as fp:
        ...
__call__(filename: Union[str, Path], mode='r', buffering=-1, encoding='utf-8', errors=None) IO[source]

Call self as a function.

get_file_path

testsuite.plugins.common.get_file_path()

Returns GetFilePathFixture instance.

class testsuite.plugins.common.GetFilePathFixture[source]

Returns path to static regular file.

__call__(filename: Union[str, Path], *, missing_ok=False) Optional[Path][source]

Call self as a function.

get_directory_path

testsuite.plugins.common.get_directory_path()

Returns GetDirectoryPathFixture instance.

class testsuite.plugins.common.GetDirectoryPathFixture[source]

Returns path to static directory.

__call__(filename: Union[str, Path], *, missing_ok=False) Optional[Path][source]

Call self as a function.

get_search_pathes

testsuite.plugins.common.get_search_pathes()

Returns GetSearchPathesFixture instance.

class testsuite.plugins.common.GetSearchPathesFixture[source]

Generates sequence of pathes for static files.

for ... in __call__(filename: Union[str, Path]) Iterator[Path][source]

Call self as a function.

static_dir

testsuite.plugins.common.static_dir(testsuite_request_directory) Path[source]

Static directory related to test path.

Returns static directory relative to test file, e.g.:

|- tests/
   |- static/ <-- base static directory for test_foo.py
   |- test_foo.py

initial_data_path

testsuite.plugins.common.initial_data_path() Tuple[Path, ...][source]

Use this fixture to override base static search path.

@pytest.fixture
def initial_data_path():
    return (
        pathlib.Path(PROJECT_ROOT) / 'tests/static',
        pathlib.Path(PROJECT_ROOT) / 'static',
    )

testsuite_get_source_path

testsuite.plugins.common.testsuite_get_source_path(path) pathlib.Path[source]

Returns source path related to given path.

def test_foo(testsuite_get_source_path):
    path = testsuite_get_source_path(__file__)
    ...

testsuite_get_source_directory

testsuite.plugins.common.testsuite_get_source_directory(path) pathlib.Path[source]

Session scope fixture. Returns source directory related to given path.

This fixture can be used to access data files in session fixtures, e.g:

|- tests/
   |- test_foo.py
   |- foo.txt
@pytest.fixture(scope='session')
def my_data(get_source_directory):
    return (
        testsuite_get_source_directory(__file__)
        .joinpath('foo.txt')
        .read_text()
    )