Skip to content

Commit

Permalink
Generate JUnit files using Jinja2 instead of junit-xml
Browse files Browse the repository at this point in the history
  • Loading branch information
seberm committed Aug 28, 2024
1 parent 9c9b407 commit 58689c1
Show file tree
Hide file tree
Showing 13 changed files with 482 additions and 62 deletions.
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ repos:
- "urllib3>=1.26.5, <2.0" # 1.26.16 / 2.0.4

# report-junit
- "junit_xml>=1.9"
- "lxml>=4.9.0"

- "typing-extensions>=4.4.0; python_version < '3.10'" # TypeAlias introduced in 3.10, Self in 3.11
- "pytest"
Expand All @@ -62,6 +62,7 @@ repos:
- "types-jinja2"
- "types-babel"
- "types-docutils"
- "types-lxml"

pass_filenames: false
args: [--config-file=pyproject.toml]
Expand Down Expand Up @@ -90,7 +91,7 @@ repos:
- "urllib3>=1.26.5, <2.0" # 1.26.16 / 2.0.4

# report-junit
- "junit_xml>=1.9"
- "lxml>=4.9.0"

- "typing-extensions>=4.4.0; python_version < '3.10'" # TypeAlias introduced in 3.10, Self in 3.11
- "pytest"
Expand All @@ -108,6 +109,7 @@ repos:
- "types-jinja2"
- "types-babel"
- "types-docutils"
- "types-lxml"

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: "0.29.1"
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ provision-virtual = [
]
provision-container = []
report-junit = [
"junit_xml>=1.9",
# Required to support XML parsing and checking the XSD schemas.
"lxml>=4.9.0",
"types-lxml",
]
report-polarion = [
"tmt[report-junit]",
Expand Down Expand Up @@ -216,7 +218,7 @@ module = [
"guestfs.*",
"html2text.*",
"fmf.*",
"junit_xml.*",
"lxml.*",
"libvirt.*",
"nitrate.*",
"pylero.*",
Expand Down
6 changes: 6 additions & 0 deletions tests/report/junit/data/custom.xml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" ?>
<tests>
{% for result in RESULTS %}
<test name="{{ result.name | e }}" value="{{ result.result.value | e }}"/>
{% endfor %}
</tests>
2 changes: 2 additions & 0 deletions tests/report/junit/data/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@
/timeout:
test: sleep 10
duration: 2s
/escape"<speci&l>_chars:
test: "echo '<speci&l>\"chars'"
3 changes: 3 additions & 0 deletions tests/report/junit/data/non-xml-custom.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for result in RESULTS %}
name='{{ result.name | e }}',value='{{ result.result.value | e }}'
{% endfor %}
52 changes: 48 additions & 4 deletions tests/report/junit/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,60 @@ rlJournalStart
rlPhaseEnd

for method in tmt; do
rlPhaseStartTest "$method"
rlPhaseStartTest "[$method] Basic format checks"
rlRun "tmt run -avr execute -h $method report -h junit --file junit.xml 2>&1 >/dev/null | tee output" 2
rlAssertGrep "2 tests passed, 2 tests failed and 2 errors" "output"
rlAssertGrep '<testsuite disabled="0" errors="2" failures="2" name="/plan" skipped="0" tests="6"' "junit.xml"
rlAssertGrep "3 tests passed, 2 tests failed and 2 errors" "output"
rlAssertGrep '00:00:00 pass /test/shell/escape"<speci&l>_chars (on default-0)' "output"
rlAssertGrep '<testsuite name="/plan" disabled="0" errors="2" failures="2" skipped="0" tests="7"' "junit.xml"
rlAssertGrep 'fail</failure>' "junit.xml"

# Test the escape of special characters
rlAssertGrep '<testcase name="/test/shell/escape&quot;&lt;speci&amp;l&gt;_chars">' "junit.xml"
rlAssertGrep '<system-out>&lt;speci&amp;l&gt;"chars' "junit.xml"

# Check there is no schema problem reported
rlAssertNotGrep 'The generated XML output is not a valid XML file or it is not valid against the XSD schema\.' "output"
rlPhaseEnd

rlPhaseStartTest "[$method] Check the flavor argument is working"
rlRun "tmt run -avr execute -h $method report -h junit --file junit.xml --flavor default 2>&1 >/dev/null | tee output" 2
rlAssertGrep "3 tests passed, 2 tests failed and 2 errors" "output"

# Check there is no schema problem reported
rlAssertNotGrep 'The generated XML output is not a valid XML file or it is not valid against the XSD schema\.' "output"
rlPhaseEnd

rlPhaseStartTest "[$method] Check the mutually exclusive arguments"
rlRun "tmt run -avr execute -h $method report -h junit --file junit.xml --flavor custom 2>&1 >/dev/null | tee output" 2
rlAssertGrep "The 'custom' flavor requires the '--template-path' argument." "output"

rlRun "tmt run -avr execute -h $method report -h junit --file junit.xml --template-path custom.xml.j2 2>&1 >/dev/null | tee output" 2
rlAssertGrep "The '--template-path' can be used only with '--flavor=custom'." "output"

rlPhaseEnd

rlPhaseStartTest "[$method] Check the 'custom' flavor with a custom XML template"
rlRun "tmt run -avr execute -h $method report -h junit --file custom-template-out.xml --template-path custom.xml.j2 --flavor custom 2>&1 >/dev/null | tee output" 2

# There must not be a schema check when using a custom flavor
rlAssertGrep "The 'custom' JUnit flavor is used, you are solely responsible for the validity of the XML schema\." "output"

rlAssertGrep '<test name="/test/beakerlib/fail" value="fail"/>' "custom-template-out.xml"
rlAssertGrep '<test name="/test/beakerlib/pass" value="pass"/>' "custom-template-out.xml"
rlAssertGrep '<test name="/test/shell/pass" value="pass"/>' "custom-template-out.xml"
rlAssertGrep '<test name="/test/shell/timeout" value="error"/>' "custom-template-out.xml"
rlAssertGrep '<test name="/test/shell/escape&quot;&lt;speci&amp;l&gt;_chars" value="pass"/>' "custom-template-out.xml"
rlPhaseEnd

rlPhaseStartTest "[$method] The 'custom' flavor with a custom **non-XML** template must not work"
rlRun "tmt run -avr execute -h $method report -h junit --file custom-template-out.xml --template-path non-xml-custom.j2 --flavor custom 2>&1 >/dev/null | tee output" 2

rlAssertGrep 'The generated XML output is not a valid XML file.' "output"
rlPhaseEnd
done

rlPhaseStartCleanup
rlRun "rm output junit.xml"
rlRun "rm output junit.xml custom-template-out.xml"
rlRun "popd"
rlPhaseEnd
rlJournalEnd
3 changes: 2 additions & 1 deletion tests/report/polarion/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ rlJournalStart
rlPhaseStartTest
rlRun "tmt run -avr execute report -h polarion --project-id RHELBASEOS --no-upload --planned-in RHEL-9.1.0 --file xunit.xml 2>&1 >/dev/null | tee output" 2
rlAssertGrep "1 test passed, 1 test failed and 1 error" "output"
rlAssertGrep '<testsuite disabled="0" errors="1" failures="1" name="/plan" skipped="0" tests="3"' "xunit.xml"
rlAssertGrep '<testsuite name="/plan" disabled="0" errors="1" failures="1" skipped="0" tests="3"' "xunit.xml"
rlAssertGrep '<property name="polarion-project-id" value="RHELBASEOS" />' "xunit.xml"
rlAssertGrep '<property name="polarion-testcase-id" value="BASEOS-10914" />' "xunit.xml"
rlAssertGrep '<property name="polarion-custom-plannedin" value="RHEL-9.1.0" />' "xunit.xml"
rlAssertGrep "Maximum test time '2s' exceeded." "xunit.xml"
rlPhaseEnd

rlPhaseStartCleanup
Expand Down
Loading

0 comments on commit 58689c1

Please sign in to comment.