Skip to content

Commit f5cb7eb

Browse files
authored
feat: add not_append_test_results flag to xray cli (#556)
refactor: move XrayPublisher to XrayInterface with optimized authentication - Rename XrayPublisher to XrayInterface for better API representation - Move authentication into XrayInterface constructor - Make base URL configurable as parameter - Add methods upload_test_results and get_jira_test_keys_from_test_execution_ticket to class - Remove standalone functions to eliminate code duplication - Optimize CLI to create XrayInterface once and reuse for all operations - Update all imports and tests accordingly
1 parent 1a03ab5 commit f5cb7eb

8 files changed

Lines changed: 901 additions & 120 deletions

File tree

docs/tools/xray.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Options:
3636
[optional][default value: None] Required when creating a new test execution ticket;
3737
optional when updating an existing one.
3838
--merge-xml-files Merge all the xml files to be send in one xml file
39+
--not-append-test-results, -na Do not append new test keys from the .xml(s) to the updated test execution,
40+
only overwrite already existing ones
3941
--help Show this message and exit.
4042

4143

@@ -69,6 +71,23 @@ You can rewrite the test execution description:
6971
xray --user USER_ID --password MY_API_KEY --url "https://xray.cloud.getxray.app/" upload --path-results path/reports/folder --test-execution-key "ABC-123" --test-execution-description "New test execution description"
7072
7173
74+
Update existing test results only (do not append new tests)
75+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76+
77+
By default, when uploading test results to an existing test execution ticket, new test results from the XML files are appended to the test execution.
78+
If you want to only update the results of tests that already exist in the test execution ticket (without adding new tests), use the ``--not-append-test-results`` flag:
79+
80+
.. code:: bash
81+
82+
xray --user USER_ID --password MY_API_KEY --url "https://xray.cloud.getxray.app/" upload --path-results path/reports/folder --test-execution-key "ABC-123" --not-append-test-results
83+
84+
This is useful when you want to:
85+
86+
- Update only specific test results without changing the test execution scope
87+
- Maintain a fixed set of tests in your test execution ticket
88+
- Prevent accidental addition of new tests to an existing test execution
89+
90+
7291
Add the Xray decorator to the test functions
7392
--------------------------------------------
7493

docs/whats_new/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ What's New In Pykiso?
44
.. toctree::
55

66
version_ongoing
7+
version_1_4_0
8+
version_1_3_4
9+
version_1_3_3
10+
version_1_3_2
11+
version_1_3_1
12+
version_1_3_0
713
version_1_2_0
814
version_0_29_0
915
version_0_28_0

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pykiso"
3-
version = "1.3.4"
3+
version = "1.4.0"
44
description = "Embedded integration testing framework."
55
authors = ["Sebastian Fischer <sebastian.fischer@de.bosch.com>"]
66
license = "Eclipse Public License - v 2.0"

src/pykiso/tool/xray/cli.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import click
66

7-
from .xray import extract_test_results, upload_test_results
7+
from .xray import XrayInterface, extract_test_results
88

99

1010
@click.group()
@@ -76,6 +76,14 @@ def cli_xray(ctx: dict, user: str, password: str, url: str) -> None:
7676
is_flag=True,
7777
required=False,
7878
)
79+
@click.option(
80+
"-na",
81+
"--not-append-test-results",
82+
help="Do not append new test keys from the .xml(s) to the updated test execution, only overwrite already existing ones",
83+
is_flag=True,
84+
default=False,
85+
required=False,
86+
)
7987
@click.pass_context
8088
def cli_upload(
8189
ctx,
@@ -84,6 +92,7 @@ def cli_upload(
8492
test_execution_description: str,
8593
test_execution_summary: str,
8694
merge_xml_files: bool,
95+
not_append_test_results: bool,
8796
) -> None:
8897
"""Upload the JUnit xml test results on xray.
8998
@@ -93,6 +102,7 @@ def cli_upload(
93102
:param test_execution_description: update the test execution ticket description - otherwise, keep current description
94103
:param test_execution_summary: update the test execution ticket summary - otherwise, keep current summary
95104
:param merge_xml_files: if True, merge the xml files, else do nothing
105+
:param not_append_test_results: if True, only overwrite the existing ones (update only), else append the new results from the .xml file(s) to the test execution
96106
97107
"""
98108
# If a new test execution ticket is being created (no key), the user should pass a description and a summary.
@@ -101,26 +111,34 @@ def cli_upload(
101111
"Creating a new test execution ticket requires both a description and a summary in the CLI options"
102112
)
103113

114+
# Create XrayInterface once for authentication optimization
115+
xray_interface = XrayInterface(
116+
base_url=ctx.obj["URL"], client_id=ctx.obj["USER"], client_secret=ctx.obj["PASSWORD"]
117+
)
118+
119+
# If the user chooses not to append new test results to an existing test execution ticket,
120+
# retrieve the existing test keys from Jira for the specified test execution ticket.
121+
# If appending is allowed, there is no need to fetch the existing test keys.
122+
if not_append_test_results and test_execution_key:
123+
print("Preparing the ticket for update...")
124+
jira_keys = xray_interface.get_jira_test_keys_from_test_execution_ticket(test_execution_key)
125+
else:
126+
jira_keys = []
127+
104128
# From the JUnit xml files found, create a list of the dictionary per test results marked with an xray decorator.
105129
path_results = Path(path_results).resolve()
106130
test_results = extract_test_results(
107131
path_results=path_results,
108132
merge_xml_files=merge_xml_files,
133+
jira_keys=jira_keys,
109134
test_execution_key=test_execution_key,
110135
test_execution_summary=test_execution_summary,
111136
test_execution_description=test_execution_description,
112137
)
113138

114139
responses = []
115140
for result in test_results:
116-
# Upload the test results into Xray
117-
responses.append(
118-
upload_test_results(
119-
base_url=ctx.obj["URL"],
120-
user=ctx.obj["USER"],
121-
password=ctx.obj["PASSWORD"],
122-
results=result,
123-
)
124-
)
141+
# Upload the test results into Xray using the same interface instance
142+
responses.append(xray_interface.upload_test_results(data=result))
125143
responses_result_str = json.dumps(responses, indent=2)
126144
print(f"The test results can be found in JIRA by: {responses_result_str}")

0 commit comments

Comments
 (0)