Skip to content

Commit a3388ad

Browse files
authored
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
1 parent 60d93f1 commit a3388ad

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ReTestItems"
22
uuid = "817f1d60-ba6b-4fd5-9520-3cf149f6a823"
3-
version = "1.23.0"
3+
version = "1.23.1"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/junit_xml.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,12 @@ function write_junit_xml(io, tc::JUnitTestCase)
262262
write_counts(io, tc.counts)
263263
write(io, ">")
264264
write_dd_tags(io, tc)
265-
if !isnothing(tc.logs)
266-
write(io, "\n\t\t<error")
267-
!isnothing(tc.error_message) && write(io, " message=", xml_markup(tc.error_message))
268-
write(io, ">", xml_content(strip(String(tc.logs))))
265+
if (tc.counts.failures + tc.counts.errors) > 0
266+
err_msg = something(tc.error_message, "Test errored but no error message was captured.")
267+
write(io, "\n\t\t<error message=", xml_markup(err_msg), ">")
268+
if !isnothing(tc.logs)
269+
write(io, xml_content(strip(String(tc.logs))))
270+
end
269271
write(io, "\n\t\t</error>")
270272
end
271273
write(io, "\n\t</testcase>")

test/junit_xml.jl

+48
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,52 @@ end
280280
end
281281
end
282282

283+
@testset "Manual JUnitTestSuite/JUnitTestCase with error no logs" begin
284+
using ReTestItems: JUnitTestCase, JUnitTestSuite
285+
using Dates: datetime2unix, DateTime
286+
287+
function get_test_suite(suite_name, test_name)
288+
ts = @testset "$test_name" begin
289+
# would rather make this false, but failing @test makes the ReTestItems test fail as well
290+
@test true
291+
end
292+
# Make the test time deterministic to make testing report output easier
293+
ts.time_start = datetime2unix(DateTime(2023, 01, 15, 16, 42))
294+
ts.time_end = datetime2unix(DateTime(2023, 01, 15, 16, 42, 30))
295+
296+
# should be able to construct a TestCase from a TestSet
297+
tc = JUnitTestCase(ts)
298+
@test tc isa JUnitTestCase
299+
300+
# once wrapped in a TestSuite, this should have enough info to write out a report
301+
suite = JUnitTestSuite(suite_name)
302+
junit_record!(suite, tc)
303+
@test suite isa JUnitTestSuite
304+
return suite
305+
end
306+
suite = get_test_suite("manual", "test")
307+
# pretend there is a failure
308+
suite.counts.failures = 1
309+
suite.testcases[1].counts.failures = 1
310+
311+
mktemp() do path, io
312+
write_junit_file(path, suite)
313+
report_string = read(io, String)
314+
expected = """
315+
<?xml version="1.0" encoding="UTF-8"?>
316+
<testsuite name="manual" timestamp="2023-01-15T16:42:00.0" time="30.0" tests="1" skipped="0" failures="1" errors="0">
317+
\t<testcase name="test" timestamp="2023-01-15T16:42:00.0" time="30.0" tests="1" skipped="0" failures="1" errors="0">
318+
\t\t<properties>
319+
\t\t<property name=\"dd_tags[test.id]\" value=\"$(repr(hash("test")))\"></property>
320+
\t\t</properties>
321+
\t\t<error message="Test errored but no error message was captured.">
322+
\t\t</error>
323+
\t</testcase>
324+
</testsuite>
325+
"""
326+
# leading/trailing whitespace isn't important
327+
@test strip(report_string) == strip(expected)
328+
end
329+
end
330+
283331
end # junit_xml.jl testset

0 commit comments

Comments
 (0)