Skip to content

Commit

Permalink
feat: add abort function (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannpoupon authored Apr 6, 2023
1 parent 81c8dfb commit f0deae6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pykiso/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
define_test_parameters,
retry_test_case,
)
from .test_coordinator.test_execution import abort
from .test_coordinator.test_suite import (
BasicTestSuiteSetup,
BasicTestSuiteTeardown,
Expand Down
15 changes: 15 additions & 0 deletions src/pykiso/test_coordinator/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""
from __future__ import annotations

import os
from typing import TYPE_CHECKING
from unittest.loader import VALID_MODULE_NAME

Expand Down Expand Up @@ -323,6 +324,20 @@ def collect_test_suites(
return list_of_test_suites


def abort(reason: str = None) -> None:
"""Quit ITF test and log an error if a reason is indicated and
if any errors occurred it logs them.
:param reason: reason to abort, defaults to None
"""
if reason:
log.critical(reason)
log.critical(
"Non recoverable error occurred during test execution, aborting test session."
)
os.kill(os.getpid(), ExitCode.ONE_OR_MORE_TESTS_RAISED_UNEXPECTED_EXCEPTION)


def execute(
config: Dict[str, Any],
report_type: str = "text",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_config_registry_and_test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import copy
import logging
import pathlib
import signal
from contextlib import nullcontext as does_not_raise
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -771,3 +772,17 @@ def test_config_registry_config_no_patching(mocker: MockerFixture, sample_config
assert mock_linker.provide_connector.call_count == 1
assert mock_linker.provide_auxiliary.call_count == 2
mock_linker._aux_cache.get_instance.assert_not_called()


def test_abort(mocker: MockerFixture, caplog):
reason = "reason"
os_kill_mock = mocker.patch("os.kill")

test_execution.abort(reason)

assert reason in caplog.text
assert (
"Non recoverable error occurred during test execution, aborting test session."
in caplog.text
)
os_kill_mock.assert_called_once()

0 comments on commit f0deae6

Please sign in to comment.