pontos.testing package

A module containing classes and functions mostly useful for creating unit tests

class pontos.testing.AsyncIteratorMock(iterable)

A class to mock an async iterator from an iterable like a list

Parameters:

iterable (Iterable[Any]) – Iterable to return values from

Example

from pontos.testing import AsyncIteratorMock

values = [1, 2, 3]
mock = AsyncIteratorMock(values)
async for value in mock:
    print(value)
pontos.testing.add_sys_path(directory)

Context Manager to add a directory path to the module search path aka. sys.path. The directory path is removed when the context manager is left.

Parameters:

directory (str | PathLike) – A os.PathLike directory to add to sys.path

Return type:

Generator[None, None, None]

Example

from pontos.helper import add_sys_path

with add_sys_path("/tmp/test-modules"):
    import mymodule
pontos.testing.ensure_unload_module(module)

A context manager to ensure that a module gets removed even if an error occurs

Parameters:

module (str | ModuleType) – Module instance or name of the Python module to unload. For example: foo.bar

Return type:

Generator[None, None, None]

Example

from pontos.helper import ensure_unload_module

with ensure_unload_module("foo.bar"):
    do_something()
pontos.testing.temp_directory(*, change_into=False, add_to_sys_path=False)

Context Manager to create a temporary directory

Parameters:
  • change_into (bool) – Set the created temporary as the current working directory. The behavior of the current working directory when leaving the context manager is undefined.

  • add_to_sys_path (bool) – Add the created temporary directory to the directories for searching for Python modules

Returns:

A path to the created temporary directory

Return type:

Generator[Path, None, None]

Example

from pontos.testing import temp_directory

with temp_directory(change_into=True) as tmp:
    new_file = tmp / "test.txt"
pontos.testing.temp_file(content=None, *, name='test.toml', change_into=False)

A Context Manager to create a temporary file within a new temporary directory. The temporary file and directory are removed when the context is exited.

Parameters:
  • content (str | bytes | None) – Content to write into the temporary file.

  • name (str) – Name of the temporary file. “test.toml” by default.

  • change_into (bool) – Adjust the current working directory to the temporary directory.

Returns:

A path to the created temporary file

Return type:

Generator[Path, None, None]

Example

from pontos.testing import temp_file

with temp_file("Lorem Ipsum", name="foo.txt") as fpath:
pontos.testing.temp_git_repository(*, user_name='Max Mustermann', user_email='max.mustermann@example.com', branch='main')

Context Manager to create a temporary git repository on the filesystem

Parameters:
  • user_name (str) – User name to configure in the repository. Default: Max Mustermann

  • user_email (str) – Email address of the user to configure in the repository. Default: max.mustermann@example.com

  • branch (str) – Branch name to create. Default: main

Returns:

A path to the created temporary git repository directory

Return type:

Generator[Path, None, None]

Example

from pontos.testing import temp_git_repository

with temp_git_repository() as repo:
    new_file = repo / "foo.txt"
    new_file.write_text("Lorem Ipsum")

    exec_git("add", "foo.txt")
pontos.testing.temp_python_module(content, *, name='foo', change_into=False)

A Context Manager to create a new Python module in a temporary directory. The temporary directory will be added to the module search path and removed from the search path when the context is exited. Also it is ensured that the module is unloaded if the context is exited.

Parameters:
  • content (str) – Python code to write into the temporary module.

  • name (str) – Name of the new Python module. By default: “foo”.

  • change_into (bool) – Adjust the current working directory to the temporary directory.

Returns:

A path to the created temporary Python module file

Return type:

Generator[Path, None, None]

Example

from pontos.testing import temp_python_module

with temp_python_module(
    "def hello(value):\n  print(f'Hello {value}')", name="world"
) as python_module_path:
    from world import hello
    hello("World")
pontos.testing.unload_module(module)

Unload a Python module

Parameters:

module (str | ModuleType) – Module instance or name of the Python module to unload. For example: foo.bar