def test_log_cleanup_step_based(mock_timestamp, scope, item_type, item_id):
    cfg = Config(endpoint="E", token="T", project="P", step_based=True)
    mock_timestamp.return_value = 123
    mock_rps = mock.create_autospec(ReportPortalService)
    mock_context, mock_func1, mock_func2 = mock.Mock(), mock.Mock, mock.Mock()
    mock_func1.__name__ = "cleanup_func1"
    mock_func2.__name__ = "cleanup_func2"
    mock_context._stack = [
        {"@layer": scope, "@cleanups": [mock_func1, mock_func2]}
    ]
    ba = BehaveAgent(cfg, mock_rps)
    ba._feature_id = "feature_id"
    ba._scenario_id = "scenario_id"
    ba._log_cleanups(mock_context, scope)
    calls = [
        mock.call(
            name="Execution of '{}' cleanup function".format(f_name),
            start_time=123,
            item_type=item_type,
            parent_item_id=item_id,
        )
        for f_name in ("cleanup_func1", "cleanup_func2")
    ]
    mock_rps.start_test_item.assert_has_calls(calls)
    assert mock_rps.finish_test_item.call_count == 2
def test_finish_failed_step_scenario_based(mock_timestamp, config):
    config.step_based = False
    mock_step = mock.Mock()
    mock_step.keyword = "keyword"
    mock_step.name = "name"
    mock_step.status.name = "failed"
    mock_step.text = None
    mock_step.table = None
    mock_step.exception.args = ["Exception message"]
    mock_step.error_message = "Error message"
    mock_timestamp.return_value = 123
    mock_rps = mock.create_autospec(ReportPortalService)
    mock_context = mock.Mock()
    ba = BehaveAgent(config, mock_rps)
    ba._scenario_id = "scenario_id"
    ba.finish_step(mock_context, mock_step)
    calls = [
        mock.call(
            item_id="scenario_id",
            time=123,
            level="ERROR",
            message="Step [keyword]: name was finished with exception.\n"
            "Exception message\nError message",
        ),
        mock.call(
            item_id="scenario_id",
            time=123,
            level="INFO",
            message="[keyword]: name. ",
        ),
    ]
    mock_rps.log.assert_has_calls(calls, any_order=True)
def test_finish_failed_step_step_based(mock_timestamp, config):
    config.step_based = True
    mock_step = mock.Mock()
    mock_step.keyword = "keyword"
    mock_step.name = "name"
    mock_step.status.name = "failed"
    mock_step.exception.args = ["Exception message"]
    mock_step.error_message = "Error massage"
    mock_timestamp.return_value = 123
    mock_rps = mock.create_autospec(ReportPortalService)
    mock_context = mock.Mock()
    ba = BehaveAgent(config, mock_rps)
    ba._step_id = "step_id"
    ba._scenario_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="FAILED", some_key="some_value"
    )
    mock_rps.log.assert_has_calls(
        [
            mock.call(
                item_id="step_id",
                time=123,
                level="ERROR",
                message="Step [keyword]: name was finished with exception.\n"
                "Exception message\nError massage",
            )
        ]
    )
def test_log_scenario_exception_default_message(mock_timestamp, config):
    mock_timestamp.return_value = 123
    mock_scenario = mock.Mock()
    mock_scenario.exception = None
    mock_scenario.error_message = None
    mock_scenario.name = "scenario_name"
    mock_rps = mock.create_autospec(ReportPortalService)
    ba = BehaveAgent(config, mock_rps)
    ba._scenario_id = "scenario_id"
    ba._log_scenario_exception(mock_scenario)
    mock_rps.log.assert_called_once_with(
        item_id="scenario_id",
        time=123,
        level="ERROR",
        message="Scenario 'scenario_name' finished with error.",
    )
def test_finish_scenario(mock_timestamp, 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(ReportPortalService)
    mock_context = mock.Mock()
    mock_context._stack = []
    ba = BehaveAgent(config, mock_rps)
    ba._scenario_id = "scenario_id"
    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,
        status=expected_status,
        some_key="some_value",
    )
def test_log_cleanup_scenario_based(mock_timestamp, config, scope, item_id):
    mock_timestamp.return_value = 123
    mock_rps = mock.create_autospec(ReportPortalService)
    mock_context, mock_func1, mock_func2 = mock.Mock(), mock.Mock, mock.Mock()
    mock_func1.__name__ = "cleanup_func1"
    mock_func2.__name__ = "cleanup_func2"
    mock_context._stack = [
        {"@layer": scope, "@cleanups": [mock_func1, mock_func2]}
    ]
    ba = BehaveAgent(config, mock_rps)
    ba._feature_id = "feature_id"
    ba._scenario_id = "scenario_id"
    ba._log_cleanups(mock_context, scope)
    calls = [
        mock.call(
            123,
            "Execution of '{}' cleanup function".format(f_name),
            level="INFO",
            item_id=item_id,
        )
        for f_name in ("cleanup_func1", "cleanup_func2")
    ]
    mock_rps.log.assert_has_calls(calls)
def test_start_step_step_based(mock_timestamp, config):
    config.step_based = True
    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(ReportPortalService)
    mock_rps.start_test_item.return_value = "step_id"
    mock_context = mock.Mock()
    ba = BehaveAgent(config, mock_rps)
    ba._scenario_id = "scenario_id"
    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,
        item_type="STEP",
        parent_item_id="scenario_id",
        description="",
        code_ref=BehaveAgent._code_ref(mock_step),
        some_key="some_value",
    )
    ba._step_id = "step_id"