Skip to content

Commit

Permalink
Merge multiple features in one XRAY result
Browse files Browse the repository at this point in the history
If there is multiple features files with the
same test plany ID and test execution ID
we should merge them in one XRAY results and publish only once #27.
  • Loading branch information
fundakol committed Aug 1, 2024
1 parent cfb8034 commit c726d19
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
23 changes: 17 additions & 6 deletions src/behave_xray/formatter.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()

Expand All @@ -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."""
Expand Down
13 changes: 13 additions & 0 deletions tests/features/calculator2.feature
Original file line number Diff line number Diff line change
@@ -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
21 changes: 17 additions & 4 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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',
Expand All @@ -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': []
}
]

0 comments on commit c726d19

Please sign in to comment.