diff --git a/CHANGELOG.md b/CHANGELOG.md index 368c6ec..eb895a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## [Unreleased] ### Added +- Microseconds precision for timestamps, by @HardNorth +### Changed +- Client version updated on [5.7.4](https://github.com/reportportal/client-Python/releases/tag/5.7.4), by @HardNorth + +## [5.1.1] +### Added - Logging on Behave "error" status, by @HardNorth ### Changed - Unknown statuses now handled as `FAILED`, by @HardNorth diff --git a/behave_reportportal/behave_agent.py b/behave_reportportal/behave_agent.py index 1359274..32de09c 100644 --- a/behave_reportportal/behave_agent.py +++ b/behave_reportportal/behave_agent.py @@ -17,6 +17,7 @@ import os import traceback from collections import defaultdict +from datetime import datetime, timezone from functools import wraps from os import PathLike from typing import Any, Callable, Optional, Union @@ -34,7 +35,6 @@ gen_attributes, get_launch_sys_attrs, get_package_version, - timestamp, ) from behave_reportportal.config import Config, LogLayout @@ -140,7 +140,7 @@ def start_launch(self, _: Context, **kwargs: Any) -> None: self._handle_lifecycle = False if self._rp.launch_uuid else True self._launch_id = self._rp.launch_uuid or self._rp.start_launch( name=self._cfg.launch_name, - start_time=timestamp(), + start_time=datetime.now(tz=timezone.utc), attributes=self._get_launch_attributes(), description=self._cfg.launch_description, rerun=self._cfg.rerun, @@ -152,7 +152,7 @@ def start_launch(self, _: Context, **kwargs: Any) -> None: def finish_launch(self, _: Context, **kwargs: Any) -> None: """Finish launch in ReportPortal.""" if self._handle_lifecycle: - self._rp.finish_launch(end_time=timestamp(), **kwargs) + self._rp.finish_launch(end_time=datetime.now(tz=timezone.utc), **kwargs) self._rp.close() @check_rp_enabled @@ -162,7 +162,7 @@ def start_feature(self, context: Context, feature: Feature, **kwargs: Any) -> No feature.skip("Marked with @skip") self._feature_id = self._rp.start_test_item( name=feature.name, - start_time=timestamp(), + start_time=datetime.now(tz=timezone.utc), item_type="SUITE", description=self._item_description(context, feature), code_ref=self._code_ref(feature), @@ -180,7 +180,7 @@ def finish_feature(self, context: Context, feature: Feature, status: Optional[st self._log_cleanups(context, "feature") self._rp.finish_test_item( item_id=self._feature_id, - end_time=timestamp(), + end_time=datetime.now(tz=timezone.utc), status=status or convert_to_rp_status(feature.status.name), **kwargs, ) @@ -192,7 +192,7 @@ def start_scenario(self, context: Context, scenario: Scenario, **kwargs: Any) -> scenario.skip("Marked with @skip") self._scenario_id = self._rp.start_test_item( name=scenario.name, - start_time=timestamp(), + start_time=datetime.now(tz=timezone.utc), item_type="STEP", parent_item_id=self._feature_id, code_ref=self._code_ref(scenario), @@ -223,7 +223,7 @@ def finish_scenario( self._log_cleanups(context, "scenario") self._rp.finish_test_item( item_id=self._scenario_id, - end_time=timestamp(), + end_time=datetime.now(tz=timezone.utc), status=status or rp_status, **kwargs, ) @@ -243,7 +243,7 @@ def start_step(self, _: Context, step: Step, **kwargs: Any) -> None: step_content = self._build_step_content(step) self._step_id = self._rp.start_test_item( name=f"[{step.keyword}]: {step.name}", - start_time=timestamp(), + start_time=datetime.now(tz=timezone.utc), item_type="STEP", parent_item_id=self._scenario_id, code_ref=self._code_ref(step), @@ -307,10 +307,13 @@ def _log( } except OSError: self._rp.log( - time=timestamp(), message=f"Attachment not found: {file_to_attach}", level="WARN", item_id=item_id + time=datetime.now(tz=timezone.utc), + message=f"Attachment not found: {file_to_attach}", + level="WARN", + item_id=item_id, ) self._rp.log( - time=timestamp(), + time=datetime.now(tz=timezone.utc), message=message, level=level, attachment=attachment, @@ -344,7 +347,7 @@ def _finish_step_step_based(self, step: Step, status: Optional[str] = None, **kw self._log_step_exception(step, self._step_id) self._rp.finish_test_item( item_id=self._step_id, - end_time=timestamp(), + end_time=datetime.now(tz=timezone.utc), status=status or rp_status, **kwargs, ) @@ -354,7 +357,7 @@ def _finish_step_scenario_based(self, step: Step, **kwargs: Any) -> None: step_content = self._build_step_content(step) self._rp.log( item_id=self._scenario_id, - time=timestamp(), + time=datetime.now(tz=timezone.utc), message=f"[{step.keyword}]: {step.name}." + (f"\n\n{step_content}" if step_content else ""), level="INFO", **kwargs, @@ -393,7 +396,7 @@ def _log_exception(self, initial_msg: str, exc_holder: BasicStatement, item_id: self._rp.log( item_id=item_id, - time=timestamp(), + time=datetime.now(tz=timezone.utc), level="ERROR", message="\n".join(message), ) @@ -419,15 +422,17 @@ def _log_fixtures( if self._cfg.log_layout is not LogLayout.SCENARIO: self._step_id = self._rp.start_test_item( name=msg, - start_time=timestamp(), + start_time=datetime.now(tz=timezone.utc), item_type=item_type, parent_item_id=parent_item_id, has_stats=False if self._cfg.log_layout is LogLayout.NESTED else True, ) - self._rp.finish_test_item(item_id=self._step_id, end_time=timestamp(), status="PASSED") + self._rp.finish_test_item( + item_id=self._step_id, end_time=datetime.now(tz=timezone.utc), status="PASSED" + ) continue self._rp.log( - timestamp(), + datetime.now(tz=timezone.utc), msg, level="INFO", item_id=parent_item_id, @@ -445,15 +450,17 @@ def _log_cleanups(self, context: Context, scope: str) -> None: if self._cfg.log_layout is not LogLayout.SCENARIO: self._step_id = self._rp.start_test_item( name=msg, - start_time=timestamp(), + start_time=datetime.now(tz=timezone.utc), item_type=item_type, parent_item_id=item_id, has_stats=False if self._cfg.log_layout is LogLayout.NESTED else True, ) - self._rp.finish_test_item(item_id=self._step_id, end_time=timestamp(), status="PASSED") + self._rp.finish_test_item( + item_id=self._step_id, end_time=datetime.now(tz=timezone.utc), status="PASSED" + ) continue self._rp.log( - timestamp(), + datetime.now(tz=timezone.utc), msg, level="INFO", item_id=item_id, diff --git a/behave_reportportal/config.py b/behave_reportportal/config.py index 6e717b3..57c5f2b 100644 --- a/behave_reportportal/config.py +++ b/behave_reportportal/config.py @@ -65,7 +65,7 @@ class Config(object): log_batch_payload_limit: int log_layout: LogLayout launch_uuid_print: bool - launch_uuid_print_output: Optional[OutputType] + launch_uuid_print_output: OutputType client_type: ClientType http_timeout: Optional[Union[tuple[float, float], float]] @@ -162,7 +162,7 @@ def __init__( self.launch_uuid_print = to_bool(launch_uuid_print or "False") launch_uuid_print_output_strip = launch_uuid_print_output.strip() if launch_uuid_print_output else "" self.launch_uuid_print_output = ( - OutputType[launch_uuid_print_output_strip.upper()] if launch_uuid_print_output_strip else None + OutputType[launch_uuid_print_output_strip.upper()] if launch_uuid_print_output_strip else OutputType.STDOUT ) client_type_strip = client_type.strip() if client_type else "" self.client_type = ClientType[client_type_strip.upper()] if client_type_strip else ClientType.SYNC diff --git a/requirements.txt b/requirements.txt index b78bb58..899feeb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -behave>=1.3.3,<2.0 -prettytable -reportportal-client~=5.7.0 +behave==1.3.3 +prettytable>=3.6.0, <=3.17.0 +reportportal-client==5.7.4 diff --git a/setup.py b/setup.py index 9f01a43..312a345 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ from setuptools import setup -__version__ = "5.1.1" +__version__ = "5.1.2" def read_file(fname): diff --git a/tests/units/test_config.py b/tests/units/test_config.py index 22a8aae..a880a3f 100644 --- a/tests/units/test_config.py +++ b/tests/units/test_config.py @@ -184,7 +184,7 @@ def test_read_config_default_values(mock_cp): expect(cfg.rerun_of is None) expect(cfg.enabled is True) expect(cfg.launch_uuid_print is False) - expect(cfg.launch_uuid_print_output is None) + expect(cfg.launch_uuid_print_output is OutputType.STDOUT) expect(cfg.client_type is ClientType.SYNC) assert_expectations() @@ -246,7 +246,7 @@ def test_launch_uuid_print(mock_cp): cfg = read_config(mock_context) assert cfg.launch_uuid_print - assert cfg.launch_uuid_print_output is None + assert cfg.launch_uuid_print_output is OutputType.STDOUT @mock.patch("behave_reportportal.config.ConfigParser", autospec=True) @@ -296,7 +296,7 @@ def test_no_launch_uuid_print(mock_cp): cfg = read_config(mock_context) assert not cfg.launch_uuid_print - assert cfg.launch_uuid_print_output is None + assert cfg.launch_uuid_print_output is OutputType.STDOUT @pytest.mark.parametrize( diff --git a/tests/units/test_rp_agent.py b/tests/units/test_rp_agent.py index 1e6f179..765ff48 100644 --- a/tests/units/test_rp_agent.py +++ b/tests/units/test_rp_agent.py @@ -20,7 +20,7 @@ from behave.model_core import Status from delayed_assert import assert_expectations, expect from prettytable import MARKDOWN, PrettyTable -from reportportal_client import BatchedRPClient, RPClient, ThreadedRPClient +from reportportal_client import BatchedRPClient, OutputType, RPClient, ThreadedRPClient from reportportal_client.logs import MAX_LOG_BATCH_PAYLOAD_SIZE from behave_reportportal.behave_agent import BehaveAgent, convert_to_rp_status, create_rp_service @@ -185,7 +185,7 @@ def test_create_rp_service_init(mock_rps): log_batch_size=20, log_batch_payload_limit=MAX_LOG_BATCH_PAYLOAD_SIZE, launch_uuid_print=False, - print_output=None, + print_output=OutputType.STDOUT, http_timeout=None, oauth_uri=None, oauth_username=None, @@ -265,9 +265,7 @@ def test_item_description(): assert_expectations() -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_launch(mock_timestamp, config): - mock_timestamp.return_value = 123 +def test_start_launch(config): mock_rps = mock.create_autospec(RPClient) mock_rps.launch_uuid = None mock_context = mock.Mock() @@ -275,7 +273,7 @@ def test_start_launch(mock_timestamp, config): ba.start_launch(mock_context, some_key="some_value") mock_rps.start_launch.assert_called_once_with( name=config.launch_name, - start_time=123, + start_time=mock.ANY, attributes=ba._get_launch_attributes(), description=config.launch_description, some_key="some_value", @@ -284,9 +282,8 @@ def test_start_launch(mock_timestamp, config): ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_launch_with_rerun(mock_timestamp): - mock_timestamp.return_value = 123 +def test_start_launch_with_rerun(): + mock_rps = mock.create_autospec(RPClient) mock_rps.launch_uuid = None mock_context = mock.Mock() @@ -303,7 +300,7 @@ def test_start_launch_with_rerun(mock_timestamp): ba.start_launch(mock_context, some_key="some_value") mock_rps.start_launch.assert_called_once_with( name=cfg.launch_name, - start_time=123, + start_time=mock.ANY, attributes=ba._get_launch_attributes(), description=cfg.launch_description, some_key="some_value", @@ -312,10 +309,9 @@ def test_start_launch_with_rerun(mock_timestamp): ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_launch_attributes(mock_timestamp, config): +def test_start_launch_attributes(config): config.launch_attributes = ["one", "two", "key:value"] - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_rps.launch_uuid = None ba = BehaveAgent(config, mock_rps) @@ -332,20 +328,18 @@ def test_start_launch_attributes(mock_timestamp, config): ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_finish_launch(mock_timestamp, config): - mock_timestamp.return_value = 123 +def test_finish_launch(config): + mock_rps = mock.create_autospec(RPClient) mock_context = mock.Mock() ba = BehaveAgent(config, mock_rps) ba.finish_launch(mock_context, some_key="some_value") - mock_rps.finish_launch.assert_called_once_with(end_time=123, some_key="some_value") + mock_rps.finish_launch.assert_called_once_with(end_time=mock.ANY, some_key="some_value") mock_rps.close.assert_called_once() -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_skip_finish_launch(mock_timestamp, config): - mock_timestamp.return_value = 123 +def test_skip_finish_launch(config): + mock_rps = mock.create_autospec(RPClient) mock_rps.launch_uuid = "abc" mock_context = mock.Mock() @@ -356,20 +350,18 @@ def test_skip_finish_launch(mock_timestamp, config): mock_rps.finish_launch.assert_not_called() -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_skipped_feature(mock_timestamp, config): +def test_start_skipped_feature(config): mock_feature = mock.Mock() mock_feature.tags = ["some_tag", "skip"] - mock_timestamp.return_value = 123 + verify_start_feature(mock_feature, config) mock_feature.skip.assert_called_once_with("Marked with @skip") -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_feature(mock_timestamp, config): +def test_start_feature(config): mock_feature = mock.Mock() mock_feature.tags = None - mock_timestamp.return_value = 123 + verify_start_feature(mock_feature, config) @@ -385,7 +377,7 @@ def verify_start_feature(mock_feature, config): # noinspection PyProtectedMember mock_rps.start_test_item.assert_called_once_with( name="feature_name", - start_time=123, + start_time=mock.ANY, item_type="SUITE", description=BehaveAgent._item_description(mock_context, mock_feature), code_ref=BehaveAgent._code_ref(mock_feature), @@ -400,12 +392,11 @@ def verify_start_feature(mock_feature, config): @pytest.mark.parametrize("tags,expected_status", [(None, "PASSED"), (["skip"], "SKIPPED")]) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_finish_feature(mock_timestamp, config, tags, expected_status): +def test_finish_feature(config, tags, expected_status): mock_feature = mock.Mock() mock_feature.tags = tags mock_feature.status.name = "passed" - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_context = mock.Mock() mock_context._stack = [] @@ -414,26 +405,24 @@ def test_finish_feature(mock_timestamp, config, tags, expected_status): ba.finish_feature(mock_context, mock_feature, some_key="some_value") mock_rps.finish_test_item.assert_called_once_with( item_id="feature_id", - end_time=123, + end_time=mock.ANY, status=expected_status, some_key="some_value", ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_skipped_scenario(mock_timestamp, config): +def test_start_skipped_scenario(config): mock_scenario = mock.Mock() mock_scenario.tags = ["some_tag", "skip"] - mock_timestamp.return_value = 123 + verify_start_scenario(mock_scenario, config) mock_scenario.skip.assert_called_once_with("Marked with @skip") -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_scenario(mock_timestamp, config): +def test_start_scenario(config): mock_scenario = mock.Mock() mock_scenario.tags = None - mock_timestamp.return_value = 123 + verify_start_scenario(mock_scenario, config) @@ -451,7 +440,7 @@ def verify_start_scenario(mock_scenario, config): # noinspection PyProtectedMember mock_rps.start_test_item.assert_called_once_with( name="scenario_name", - start_time=123, + start_time=mock.ANY, item_type="STEP", parent_item_id="feature_id", description=BehaveAgent._item_description(mock_context, mock_scenario), @@ -468,12 +457,11 @@ def verify_start_scenario(mock_scenario, config): @pytest.mark.parametrize("tags,expected_status", [(None, "PASSED"), (["skip"], "SKIPPED")]) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_finish_scenario(mock_timestamp, config, tags, expected_status): +def test_finish_scenario(config, tags, expected_status): mock_scenario = mock.Mock() mock_scenario.tags = tags mock_scenario.status.name = "passed" - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_context = mock.Mock() mock_context._stack = [] @@ -482,7 +470,7 @@ def test_finish_scenario(mock_timestamp, config, tags, expected_status): ba.finish_scenario(mock_context, mock_scenario, some_key="some_value") mock_rps.finish_test_item.assert_called_once_with( item_id="scenario_id", - end_time=123, + end_time=mock.ANY, status=expected_status, some_key="some_value", ) @@ -528,15 +516,14 @@ def test_finish_failed_scenario_step_based(mock_log, mock_start_step, mock_finis mock_finish_step.assert_called_once_with(mock_context, mock_skipped_step) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_step_step_based(mock_timestamp, config): +def test_start_step_step_based(config): config.log_layout = LogLayout.STEP mock_step = mock.Mock() mock_step.keyword = "keyword" mock_step.name = "name" mock_step.text = None mock_step.table = None - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_rps.start_test_item.return_value = "step_id" mock_context = mock.Mock() @@ -545,7 +532,7 @@ def test_start_step_step_based(mock_timestamp, config): ba.start_step(mock_context, mock_step, some_key="some_value") mock_rps.start_test_item.assert_called_once_with( name="[keyword]: name", - start_time=123, + start_time=mock.ANY, item_type="STEP", parent_item_id="scenario_id", has_stats=True, @@ -556,15 +543,14 @@ def test_start_step_step_based(mock_timestamp, config): ba._step_id = "step_id" -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_start_step_nested_based(mock_timestamp, config): +def test_start_step_nested_based(config): config.log_layout = LogLayout.NESTED mock_step = mock.Mock() mock_step.keyword = "keyword" mock_step.name = "name" mock_step.text = "step text" mock_step.table = None - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_rps.start_test_item.return_value = "step_id" mock_context = mock.Mock() @@ -573,7 +559,7 @@ def test_start_step_nested_based(mock_timestamp, config): ba.start_step(mock_context, mock_step, some_key="some_value") mock_rps.start_test_item.assert_called_once_with( name="[keyword]: name", - start_time=123, + start_time=mock.ANY, item_type="STEP", parent_item_id="scenario_id", code_ref=BehaveAgent._code_ref(mock_step), @@ -582,7 +568,7 @@ def test_start_step_nested_based(mock_timestamp, config): some_key="some_value", ) mock_rps.log.assert_called_once_with( - time=123, + time=mock.ANY, message="```\nstep text\n```\n", level="INFO", attachment=None, @@ -600,24 +586,22 @@ def test_start_step_scenario_based(config): mock_rps.start_test_item.assert_not_called() -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_finish_passed_step_step_based(mock_timestamp, config): +def test_finish_passed_step_step_based(config): config.log_layout = LogLayout.STEP mock_step = mock.Mock() mock_step.status.name = "passed" - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_context = mock.Mock() ba = BehaveAgent(config, mock_rps) ba._step_id = "step_id" ba.finish_step(mock_context, mock_step, some_key="some_value") mock_rps.finish_test_item.assert_called_once_with( - item_id="step_id", end_time=123, status="PASSED", some_key="some_value" + item_id="step_id", end_time=mock.ANY, status="PASSED", some_key="some_value" ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_finish_failed_step_step_based(mock_timestamp, config): +def test_finish_failed_step_step_based(config): try: raise AssertionError("error!") except AssertionError as e: @@ -630,7 +614,7 @@ def test_finish_failed_step_step_based(mock_timestamp, config): mock_step.exception = e mock_step.exc_traceback = e_traceback mock_step.error_message = "Error message" - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_context = mock.Mock() ba = BehaveAgent(config, mock_rps) @@ -639,18 +623,17 @@ def test_finish_failed_step_step_based(mock_timestamp, config): ba.finish_step(mock_context, mock_step, some_key="some_value") mock_rps.finish_test_item.assert_called_once_with( item_id="step_id", - end_time=123, + end_time=mock.ANY, status="FAILED", some_key="some_value", ) formatted_exception = "".join(traceback.format_exception(type(e), e, e_traceback)) expected_msg = "Step [keyword]: name was finished with exception.\n" f"{formatted_exception}\nError message" - expected_calls = [mock.call(item_id="step_id", time=123, level="ERROR", message=expected_msg)] + expected_calls = [mock.call(item_id="step_id", time=mock.ANY, level="ERROR", message=expected_msg)] mock_rps.log.assert_has_calls(expected_calls) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_finish_failed_step_scenario_based(mock_timestamp, config): +def test_finish_failed_step_scenario_based(config): try: raise AssertionError("error!") except AssertionError as e: @@ -666,7 +649,7 @@ def test_finish_failed_step_scenario_based(mock_timestamp, config): mock_step.exception.args = tuple("Exception message") mock_step.exc_traceback = e_traceback mock_step.error_message = "Error message" - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_context = mock.Mock() ba = BehaveAgent(config, mock_rps) @@ -677,13 +660,13 @@ def test_finish_failed_step_scenario_based(mock_timestamp, config): calls = [ mock.call( item_id="scenario_id", - time=123, + time=mock.ANY, level="ERROR", message=expected_msg, ), mock.call( item_id="scenario_id", - time=123, + time=mock.ANY, level="INFO", message="[keyword]: name.", ), @@ -691,9 +674,8 @@ def test_finish_failed_step_scenario_based(mock_timestamp, config): mock_rps.log.assert_has_calls(calls, any_order=True) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_log_exception_without_message(mock_timestamp, config): - mock_timestamp.return_value = 123 +def test_log_exception_without_message(config): + mock_step = mock.Mock() mock_step.exception = None mock_step.error_message = None @@ -704,7 +686,7 @@ def test_log_exception_without_message(mock_timestamp, config): ba._log_step_exception(mock_step, "step_id") mock_rps.log.assert_called_once_with( item_id="step_id", - time=123, + time=mock.ANY, level="ERROR", message="Step [keyword]: name was finished with exception.", ) @@ -735,16 +717,15 @@ def test_post_launch_log(mock_log, config): @mock.patch("behave_reportportal.behave_agent.mimetypes") -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_post__log(mock_timestamp, mock_mime, config): - mock_timestamp.return_value = 123 +def test_post__log(mock_mime, config): + mock_rps = mock.create_autospec(RPClient) ba = BehaveAgent(config, mock_rps) mock_mime.guess_type.return_value = ("mime_type", None) with mock.patch("builtins.open", mock.mock_open(read_data="data")): ba._log("message", "ERROR", file_to_attach="filepath", item_id="item_id") mock_rps.log.assert_called_once_with( - time=123, + time=mock.ANY, message="message", level="ERROR", attachment={ @@ -783,9 +764,8 @@ def test_build_text_content(mock_init): assert text == "```\nStep text\n```\n" -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_log_scenario_exception_default_message(mock_timestamp, config): - mock_timestamp.return_value = 123 +def test_log_scenario_exception_default_message(config): + mock_scenario = mock.Mock() mock_scenario.exception = None mock_scenario.error_message = None @@ -796,19 +776,18 @@ def test_log_scenario_exception_default_message(mock_timestamp, config): ba._log_scenario_exception(mock_scenario) mock_rps.log.assert_called_once_with( item_id="scenario_id", - time=123, + time=mock.ANY, level="ERROR", message="Scenario 'scenario_name' finished with error.", ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_log_scenario_exception(mock_timestamp, config): +def test_log_scenario_exception(config): try: raise ValueError("error!") except ValueError as e: e_traceback = sys.exc_info()[2] - mock_timestamp.return_value = 123 + mock_scenario = mock.Mock() mock_scenario.exception = e mock_scenario.exc_traceback = e_traceback @@ -822,7 +801,7 @@ def test_log_scenario_exception(mock_timestamp, config): expected_msg = "Scenario 'scenario_name' finished with error.\n" f"{formatted_exception}\nError message" mock_rps.log.assert_called_once_with( item_id="scenario_id", - time=123, + time=mock.ANY, level="ERROR", message=expected_msg, ) @@ -838,9 +817,8 @@ def test_log_fixtures_without_fixture_tags(tags, config): mock_rps.start_test_item.assert_not_called() -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_log_fixtures(mock_timestamp): - mock_timestamp.return_value = 123 +def test_log_fixtures(): + cfg = Config( endpoint="endpoint", token="token", @@ -854,7 +832,7 @@ def test_log_fixtures(mock_timestamp): mock_rps.log.assert_has_calls( [ mock.call( - 123, + mock.ANY, f"Using of '{t}' fixture", level="INFO", item_id="item_id", @@ -868,7 +846,7 @@ def test_log_fixtures(mock_timestamp): mock_rps.start_test_item.assert_has_calls( [ mock.call( - start_time=123, + start_time=mock.ANY, name=f"Using of '{t}' fixture", item_type="type", parent_item_id="item_id", @@ -908,10 +886,9 @@ def test_log_cleanup_no_cleanups(config): ("scenario", "AFTER_TEST", "scenario_id"), ], ) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_log_cleanup_step_based(mock_timestamp, scope, item_type, item_id): +def test_log_cleanup_step_based(scope, item_type, item_id): cfg = Config(endpoint="E", token="T", project="P", log_layout=LogLayout.STEP) - mock_timestamp.return_value = 123 + mock_rps = mock.create_autospec(RPClient) mock_context, mock_func1, mock_func2 = mock.Mock(), mock.Mock(), mock.Mock() mock_func1.__name__ = "cleanup_func1" @@ -924,7 +901,7 @@ def test_log_cleanup_step_based(mock_timestamp, scope, item_type, item_id): calls = [ mock.call( name=f"Execution of '{f_name}' cleanup function", - start_time=123, + start_time=mock.ANY, item_type=item_type, parent_item_id=item_id, has_stats=True, @@ -936,9 +913,8 @@ def test_log_cleanup_step_based(mock_timestamp, scope, item_type, item_id): @pytest.mark.parametrize("scope,item_id", [("feature", "feature_id"), ("scenario", "scenario_id")]) -@mock.patch("behave_reportportal.behave_agent.timestamp") -def test_log_cleanup_scenario_based(mock_timestamp, config, scope, item_id): - mock_timestamp.return_value = 123 +def test_log_cleanup_scenario_based(config, scope, item_id): + mock_rps = mock.create_autospec(RPClient) mock_context, mock_func1, mock_func2 = mock.Mock(), mock.Mock(), mock.Mock() mock_func1.__name__ = "cleanup_func1" @@ -950,7 +926,7 @@ def test_log_cleanup_scenario_based(mock_timestamp, config, scope, item_id): ba._log_cleanups(mock_context, scope) calls = [ mock.call( - 123, + mock.ANY, f"Execution of '{f_name}' cleanup function", level="INFO", item_id=item_id,