Skip to content

Commit

Permalink
QE-16383 Add report path in JUnit when available (#513)
Browse files Browse the repository at this point in the history
## PR Type
What kind of change does this PR introduce?

<!-- Please check the one that applies to this PR using "x". -->
- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting)
- [ ] Refactoring (no functional changes)
- [ ] CI related changes
- [ ] Other... Please describe:

## What is the current behavior?
When html report is generated, JUnit file doesn't have the path and it's
up to `domino-testrail` reporter to construct the path. Therefore,
whenever we change the path, we need to update `domino-testrail`

Issue Number: https://dominodatalab.atlassian.net/browse/QE-16383

## What is the new behavior?
When html report is generated, the path is added to the JUnit file and
`domino-testrail` can get the info directly.

## Does this PR introduce a breaking change?
- [ ] Yes
- [x] No

<!-- If this PR contains a breaking change, please describe the impact
and migration path for existing applications below. -->

## Other information
  • Loading branch information
ddl-xin authored Aug 13, 2024
1 parent a8f4ff1 commit 96d042e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project closely adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.201.0
- change - add report path to the JUnit files when available

## 0.200.0
- change - increase the log level of scheduling a feature file

Expand Down
15 changes: 15 additions & 0 deletions features/cli/report_basics.feature
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,18 @@ Feature: Report basics
When I click the button "Just a scenario that opens a web page"
And I wait to click the button "show images"
And I should see the image with the alt text "After I should see the text inexistent"

Scenario: User can run a basic test and create a report with the report path in JUnit
Given I run the command "cucu run data/features/echo.feature --results {CUCU_RESULTS_DIR}/junit-results" and expect exit code "0"
And I run the command "cucu report {CUCU_RESULTS_DIR}/junit-results --output {CUCU_RESULTS_DIR}/junit-report --junit {CUCU_RESULTS_DIR}/junit-results" and expect exit code "0"
And I should see the file at "{CUCU_RESULTS_DIR}/junit-results/Echo.xml" contains the following
"""
report_path="results/junit-report/Echo/Echo an environment variable/index.html"
"""

Scenario: User can run a basic test with report option and have the report path in JUnit
Given I run the command "cucu run data/features/echo.feature --results {CUCU_RESULTS_DIR}/junit-generate-results --generate-report --report {CUCU_RESULTS_DIR}/junit-generate-report" and expect exit code "0"
And I should see the file at "{CUCU_RESULTS_DIR}/junit-generate-results/Echo.xml" contains the following
"""
report_path="results/junit-generate-report/Echo/Echo an environment variable/index.html"
"""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cucu"
version = "0.200.0"
version = "0.201.0"
license = "MIT"
description = "Easy BDD web testing"
authors = ["Domino Data Lab <[email protected]>"]
Expand Down
48 changes: 43 additions & 5 deletions src/cucu/cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import signal
import time
import xml.etree.ElementTree as ET
from importlib.metadata import version
from threading import Timer

Expand Down Expand Up @@ -453,19 +454,24 @@ def runtime_exit():

if generate_report:
_generate_report(
results, report, only_failures=report_only_failures
results, report, only_failures=report_only_failures, junit=junit
)


def _generate_report(filepath, output, only_failures: False):
def _generate_report(
filepath: str, output: str, only_failures: False, junit: str | None = None
):
"""
helper method to handle report generation so it can be used by the `cucu report`
command also the `cucu run` when told to generate a report.
command also the `cucu run` when told to generate a report. If junit is provided, it adds report
path to the JUnit files.
parameters:
filepath(string): the results directory containing the previous test run
output(string): the directory where we'll generate the report
only_failures(bool, optional): if only report failures. The default is False.
junit(str|None, optional): the directory of the JUnit files. The default if None.
"""
if os.path.exists(output):
shutil.rmtree(output)
Expand All @@ -477,6 +483,25 @@ def _generate_report(filepath, output, only_failures: False):
)
print(f"HTML test report at {report_location}")

if junit:
_add_report_path_in_junit(junit, output)


def _add_report_path_in_junit(junit_folder, report_folder):
for junit_file in glob.glob(f"{junit_folder}/*.xml", recursive=True):
junit = ET.parse(junit_file)
test_suite = junit.getroot()
ts_folder = test_suite.get("foldername")
for test_case in test_suite.iter("testcase"):
report_path = os.path.join(
report_folder,
ts_folder,
test_case.get("foldername"),
"index.html",
)
test_case.set("report_path", report_path)
junit.write(junit_file, encoding="utf-8", xml_declaration=False)


@main.command()
@click.argument("filepath", default="results")
Expand Down Expand Up @@ -505,8 +530,21 @@ def _generate_report(filepath, output, only_failures: False):
help="when set status output is shown (helpful for CI that wants stdout updates)",
)
@click.option("-o", "--output", default="report")
@click.option(
"-j",
"--junit",
default=None,
help="specify the output directory for JUnit XML files, default is "
"the same location as --results",
)
def report(
filepath, only_failures, logging_level, show_skips, show_status, output
filepath,
only_failures,
logging_level,
show_skips,
show_status,
output,
junit,
):
"""
generate a test report from a results directory
Expand Down Expand Up @@ -535,7 +573,7 @@ def report(
# initialize any underlying custom step code things
behave_init(run_details["filepath"])

_generate_report(filepath, output, only_failures=only_failures)
_generate_report(filepath, output, only_failures=only_failures, junit=junit)


@main.command()
Expand Down

0 comments on commit 96d042e

Please sign in to comment.