pytest_multilog.helper

Main module providing common test class with miscellaneous utilities and fixtures

Classes

TestHelper

Base class for tests, providing common utilities and fixtures.

Module Contents

class pytest_multilog.helper.TestHelper

Base class for tests, providing common utilities and fixtures.

Test classes can simply inherit from this class to get access to them.

property root_folder: pathlib.Path

Root test folder (i.e. pytest root folder)

property output_folder: pathlib.Path

Output folder for the whole project (defaults to “<root folder>/out/tests”).

property test_folder: pathlib.Path

Test folder for the current test (defaults to “<output folder>/__running__/<worker>/<test name>”).

property test_logs: pathlib.Path

Path to the current test logs file (i.e. pytest.log in the test folder)

check_logs(expected: str | re.Pattern[str] | list[str | re.Pattern[str]], timeout: int | None = None, check_order: bool = False)

Verify if expected pattern(s) can be found in current test logs.

expected: may be:
  • a string (simple “contains” check)

  • a Pattern object (line match with regular expression)

  • a list of both (all list items must match)

If timeout is provided, loop until either the pattern(s) is/are found or the timeout expires.

If check_order is True, patterns will be verified in specified order; order doesn’t matter otherwise

Parameters:
  • expected – Expected pattern(s) to find in logs

  • timeout – Timeout in seconds (None for no timeout)

  • check_order – If True, patterns must be found in specified order

Raises:

AssertionError – If some pattern is not found (after timeout if specified, else immediately)

property test_final_folder: pathlib.Path

Final test folder for the current test, once execution is complete (defaults to “<output folder>/<test name>”)

property test_module: str

Test module, from which test file path is computed (defaults to ‘tests’) when building test name.

If set to None or empty string, test file path will not be included in test name.

property filter_test_prefixes: bool

If True (default), “test_” and “Test” prefixes are removed from test name parts.

property test_name: str

Test name, computed to be unique for each test, and to reflect test file path, class name, method name, and parameters (if any).

Example: for a test method named “test_simple” in class “TestTheHelper” in file “tests/subtests/test_fromsubmodule.py”,

the test name will be “subtests/fromsubmodule/TheHelper/simple”. If the test method is parameterized:

  • parameters will be included in the method name (e.g. “test_simple[param1-param2]”)

  • parameters values are sanitized to be valid folder names

property worker: str

Worker name in parallelized tests: master for non-parallelized tests, or e.g. “gw0”, “gw1”, etc. for parallelized tests.

property worker_index: int

Worker index as integer (0 for master or “gw0”, 1 for “gw1”, etc.).

logs_folder_setup(request: _pytest.fixtures.FixtureRequest) → collections.abc.Generator[pathlib.Path, None, None]

Logs folder setup fixture

  • sets the root_folder attribute

  • clean test/final folders

  • yield to test

  • moves test folder to final folder after test completion

default_multilog_format() → str

Message format for default multilog handler instance

default_multilog_date_format() → str

Date format for default multilog handler instance

default_multilog_handler(logs_folder_setup: pathlib.Path, default_multilog_format: str, default_multilog_date_format: str) → logging.FileHandler

Default multi-process logging handler fixture (pytest.log in test folder).

extra_multilog_handlers() → list[logging.Handler]

Extra multi-process logging handlers fixture, providing a list of extra logging handlers to install (empty by default).

logs(logs_folder_setup: pathlib.Path, default_multilog_handler: logging.FileHandler, extra_multilog_handlers: list[logging.Handler])

Main logging setup fixture, automatically called for each test, to prepare test folder and logs file, and to install logging handler.