From a3388ad442ac58dac395424ebcdf849939ac6515 Mon Sep 17 00:00:00 2001 From: Rob Vermaas Date: Mon, 29 Jan 2024 11:37:44 +0100 Subject: [PATCH] Add error element when error/failures happens, even if there are no logs available. (#144) * Add error element when error/failures happens, even if there are no logs available. Needed for Datadog * Update version --- Project.toml | 2 +- src/junit_xml.jl | 10 ++++++---- test/junit_xml.jl | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 2271b798..91196885 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ReTestItems" uuid = "817f1d60-ba6b-4fd5-9520-3cf149f6a823" -version = "1.23.0" +version = "1.23.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/junit_xml.jl b/src/junit_xml.jl index a1d28bb6..3ca781a3 100644 --- a/src/junit_xml.jl +++ b/src/junit_xml.jl @@ -262,10 +262,12 @@ function write_junit_xml(io, tc::JUnitTestCase) write_counts(io, tc.counts) write(io, ">") write_dd_tags(io, tc) - if !isnothing(tc.logs) - write(io, "\n\t\t", xml_content(strip(String(tc.logs)))) + if (tc.counts.failures + tc.counts.errors) > 0 + err_msg = something(tc.error_message, "Test errored but no error message was captured.") + write(io, "\n\t\t") + if !isnothing(tc.logs) + write(io, xml_content(strip(String(tc.logs)))) + end write(io, "\n\t\t") end write(io, "\n\t") diff --git a/test/junit_xml.jl b/test/junit_xml.jl index 271adbd5..31992255 100644 --- a/test/junit_xml.jl +++ b/test/junit_xml.jl @@ -280,4 +280,52 @@ end end end +@testset "Manual JUnitTestSuite/JUnitTestCase with error no logs" begin + using ReTestItems: JUnitTestCase, JUnitTestSuite + using Dates: datetime2unix, DateTime + + function get_test_suite(suite_name, test_name) + ts = @testset "$test_name" begin + # would rather make this false, but failing @test makes the ReTestItems test fail as well + @test true + end + # Make the test time deterministic to make testing report output easier + ts.time_start = datetime2unix(DateTime(2023, 01, 15, 16, 42)) + ts.time_end = datetime2unix(DateTime(2023, 01, 15, 16, 42, 30)) + + # should be able to construct a TestCase from a TestSet + tc = JUnitTestCase(ts) + @test tc isa JUnitTestCase + + # once wrapped in a TestSuite, this should have enough info to write out a report + suite = JUnitTestSuite(suite_name) + junit_record!(suite, tc) + @test suite isa JUnitTestSuite + return suite + end + suite = get_test_suite("manual", "test") + # pretend there is a failure + suite.counts.failures = 1 + suite.testcases[1].counts.failures = 1 + + mktemp() do path, io + write_junit_file(path, suite) + report_string = read(io, String) + expected = """ + + + \t + \t\t + \t\t + \t\t + \t\t + \t\t + \t + + """ + # leading/trailing whitespace isn't important + @test strip(report_string) == strip(expected) + end +end + end # junit_xml.jl testset