diff --git a/src/behave_xray/formatter.py b/src/behave_xray/formatter.py index 8cdebdd..f71cc17 100644 --- a/src/behave_xray/formatter.py +++ b/src/behave_xray/formatter.py @@ -1,11 +1,10 @@ import importlib import json -import sys from collections import defaultdict from dataclasses import dataclass, field from enum import Enum, auto from os import environ, getenv -from typing import AnyStr, Dict, List, Optional, Tuple, Union +from typing import Any, AnyStr, Dict, List, Optional, Tuple, Union import pluggy from behave.formatter.base import Formatter @@ -95,6 +94,8 @@ def __init__(self, stream, config, publisher: XrayPublisher): ) # store Jira Xray test ID with corresponding Behave's scenario self.testcases: Dict[str, ScenarioResult] = defaultdict(lambda: ScenarioResult()) + # stores results for same test plan and test execution + self._results: Dict[Tuple[str, str], Dict[str, Any]] = {} def _get_plugin_manager(self): pm = pluggy.PluginManager('xray') @@ -227,10 +228,11 @@ def eof(self) -> None: self.collect_tests() if self.test_execution.tests: - self.xray_publisher.publish(self.test_execution.as_dict()) - if self.stream != sys.stdout: - self.stream.write(json.dumps(self.test_execution.as_dict(), indent=4)) - self.stream.flush() + key = (self.test_execution.test_plan_key, self.test_execution.test_execution_key) + if key not in self._results: + self._results[key] = self.test_execution.as_dict() + else: + self._results[key]['tests'].extend(self.test_execution.as_dict()['tests']) self.test_execution.flush() self.reset() @@ -247,6 +249,15 @@ def collect_tests(self) -> None: testcase.evidences = tc_status.evidences self.test_execution.append(testcase) + def close(self): + all_results = [] + for results in self._results.values(): + self.xray_publisher.publish(results) + all_results.append(results) + self.stream.write(json.dumps(all_results, indent=4)) + self.stream.flush() + self.close_stream() + class XrayFormatter(_XrayFormatterBase): """Formatter publish test results to Jira Xray.""" diff --git a/tests/features/calculator2.feature b/tests/features/calculator2.feature new file mode 100644 index 0000000..eee65bd --- /dev/null +++ b/tests/features/calculator2.feature @@ -0,0 +1,13 @@ +@jira.test_plan('JIRA-1') +Feature: Calculator2 + Test if adding two numbers returns proper result. + + @allure.testcase('JIRA-41') + Scenario: Add two numbers should pass + When I add 4 and 5 + Then result is 9 + + @jira.testcase('JIRA-42') + Scenario: Add two numbers should failed + When I add 4 and 6 + Then result is 9 diff --git a/tests/integration_test.py b/tests/integration_test.py index 9bcc1dc..812a799 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -25,7 +25,7 @@ def test_if_xray_formatter_publishes_results(formatter, auth_type, auth): ) assert not process.stderr assert 'Uploaded results to JIRA XRAY Test Execution: JIRA-1000' in process.stdout, process.stdout - assert '4 scenarios passed, 2 failed, 0 skipped' in process.stdout, process.stdout + assert '5 scenarios passed, 3 failed, 0 skipped' in process.stdout, process.stdout def test_if_xray_formatter_results_matches_expected_format(auth, tmp_path): @@ -42,13 +42,14 @@ def test_if_xray_formatter_results_matches_expected_format(auth, tmp_path): print(process.stdout) assert not process.stderr assert 'Uploaded results to JIRA XRAY Test Execution: JIRA-1000' in process.stdout - assert '4 scenarios passed, 2 failed, 0 skipped' in process.stdout + assert '5 scenarios passed, 3 failed, 0 skipped' in process.stdout with open(report_path.name, 'r') as f: report = json.load(f) - assert 'tests' in report - assert report['tests'] == [ + assert len(report) == 1 + assert 'tests' in report[0] + assert report[0]['tests'] == [ { 'testKey': 'JIRA-31', 'status': 'PASS', @@ -72,5 +73,17 @@ def test_if_xray_formatter_results_matches_expected_format(auth, tmp_path): 'status': 'FAIL', 'comment': '', # FIXME: missing assertion message 'examples': ['PASS', 'FAIL'] + }, + { + 'testKey': 'JIRA-41', + 'status': 'PASS', + 'comment': '', + 'examples': [] + }, + { + 'testKey': 'JIRA-42', + 'status': 'FAIL', + 'comment': 'Assertion Failed: Not equal', + 'examples': [] } ]