Skip to content

Commit

Permalink
Add action to timeout (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shutgun authored Mar 22, 2022
1 parent 0b16e5a commit 82913c1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.2.0] - 2022/03/22

### Added

* Choose an action and a message on timeout

### Fixed

* Docstrings of actions

## [5.1.4] - 2022/02/22

### Fixed

* Testcases which are skipped due to test_case_keys are no longer reported to adaptavist


## [5.1.3] - 2022/02/14

### Fixed
Expand Down
7 changes: 5 additions & 2 deletions pytest_adaptavist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ def meta_block(request: pytest.FixtureRequest) -> MetaBlockFixture:
"""

def get_meta_block(step: int | None = None, timeout: int = META_BLOCK_TIMEOUT) -> MetaBlock:
def get_meta_block(step: int | None = None,
timeout: int = META_BLOCK_TIMEOUT,
action_on_timout: MetaBlock.Action = MetaBlock.Action.STOP_METHOD,
message_on_timeout: str = "The test step exceeded its timewindow and timed out.") -> MetaBlock:
"""Return a meta block context to process single test blocks/steps."""
return MetaBlock(request, timeout=timeout, step=step)
return MetaBlock(request, timeout=timeout, action_on_timout=action_on_timout, message_on_timeout=message_on_timeout, step=step)

return get_meta_block
21 changes: 11 additions & 10 deletions pytest_adaptavist/metablock.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ class Action(IntEnum):
"""Action to take, if a test case fails."""

NONE = 0
"""If condition fails, collect assumption, set block/test to 'Fail' and continue (just like 'assume')."""
FAIL_CONTEXT = 0
"""If condition fails, skip execution of this block, set it to 'Blocked' and continue with next block."""
"""If condition fails, collect assumption, set block/test to 'Fail' and continue (just like 'assume')."""
STOP_CONTEXT = 1
"""If condition fails, skip execution of this block/test, set it to 'Fail' and continue with next test (just like 'assert')."""
"""If condition fails, skip execution of this block, set it to 'Blocked' and continue with next block."""
FAIL_METHOD = 2
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and continue with next test."""
"""If condition fails, skip execution of this block/test, set it to 'Fail' and continue with next test (just like 'assert')."""
STOP_METHOD = 3
"""If condition fails, skip execution of this block/test, set it to 'Fail' and block following tests."""
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and continue with next test."""
FAIL_SESSION = 4
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and block following tests as well."""
"""If condition fails, skip execution of this block/test, set it to 'Fail' and block following tests."""
STOP_SESSION = 5
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and exit session."""
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and block following tests as well."""
FAIL_EXIT_SESSION = 6
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and exit session."""
STOP_EXIT_SESSION = 7
"""If condition fails, skip execution of this block/test, set it to 'Blocked' and exit session."""

def __init__(self, request: pytest.FixtureRequest, timeout: int, step: int | None = None):
def __init__(self, request: pytest.FixtureRequest, timeout: int, action_on_timout: Action, message_on_timeout: str, step: int | None = None):
fullname = get_item_nodeid(request.node)
self.item = request.node
self.items = request.session.items
Expand All @@ -59,6 +59,8 @@ def __init__(self, request: pytest.FixtureRequest, timeout: int, step: int | Non
self.start = datetime.now().timestamp()
self.stop = datetime.now().timestamp()
self.timeout = timeout
self.action_on_timeout = action_on_timout
self.message_on_timeout = message_on_timeout
self.adaptavist: PytestAdaptavist = request.config.pluginmanager.getplugin("_adaptavist")
self.data: dict[str, Any] = self.adaptavist.test_result_data.setdefault(fullname + ("_" + str(step) if step else ""), {
"comment": None, "attachment": None
Expand All @@ -84,8 +86,7 @@ def __exit__(self, exc_type: type, exc_value: Exception, traceback: TracebackTyp
self.stop = datetime.now().timestamp()
fullname = get_item_nodeid(self.item)
if exc_type is TimeoutError:
self.data["blocked"] = True
pytest.skip(msg=f"Blocked. {self.item_name} failed: The test step exceeded its timewindow and timed out")
self._process_failed_condition(self.action_on_timeout, self.message_on_timeout)
skip_status = self.item.get_closest_marker("block") or self.item.get_closest_marker("skip")

# if method was blocked dynamically (during call) an appropriate marker is used
Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_T16(self):
""")
_, etrs, _ = adaptavist_mock
pytester.runpytest("--adaptavist")
etrs.call_args.kwargs["test_case_key"] == "MARKER-T16"
assert etrs.call_args.kwargs["test_case_key"] == "MARKER-T16"

@pytest.mark.usefixtures("configure")
def test_respect_project_decorator_if_project_key_set(self, pytester: pytest.Pytester, adaptavist_mock: AdaptavistMock):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_metablock.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ def test_TEST_T123(meta_block):
outcome = pytester.runpytest("--adaptavist").parseoutcomes()
assert outcome["skipped"] == 1

@pytest.mark.usefixtures("adaptavist_mock")
def test_meta_block_timeout_fail(self, pytester: pytest.Pytester):
"""Test if a meta block is timed out with fail action."""
pytester.makepyfile("""
from time import sleep
import pytest
def test_TEST_T123(meta_block):
with meta_block(1, timeout=1, action_on_timeout=0):
sleep(2)
assert True
""")
outcome = pytester.runpytest("--adaptavist").parseoutcomes()
assert outcome["failed"] == 1

@pytest.mark.usefixtures("adaptavist_mock")
def test_meta_block_check_unknown_arguments(self, pytester: pytest.Pytester):
"""Test if meta_block handles unknown keyword arguments correctly."""
Expand Down

0 comments on commit 82913c1

Please sign in to comment.