Skip to content

Commit

Permalink
Handle skipped test cases in JUnit output
Browse files Browse the repository at this point in the history
Split test case element into different types.
  • Loading branch information
icy-arctic-fox committed Jul 28, 2024
1 parent 265a132 commit 8a7042c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 30 deletions.
19 changes: 13 additions & 6 deletions src/spectator/formatters/compatible_junit_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,20 @@ module Spectator::Formatters
location = result.example.location
class_name = construct_class_name(location)

@test_cases << JUnit::TestCase.new(
name: result.example.full_description || "<Anonymous>",
properties = {
name: result.example.full_description || "<Anonymous>",
class_name: class_name,
file: location.try &.file,
line: location.try &.line,
error: result.exception?,
)
file: location.try &.file,
line: location.try &.line,
}

@test_cases << if result.failed?
JUnit::FailedTestCase.new(**properties, error: result.exception)
elsif result.skipped?
JUnit::SkippedTestCase.new(**properties, skip_message: result.error_message)
else
JUnit::PassedTestCase.new(**properties)
end

@failure_count += 1 if result.failed?
@skipped_count += 1 if result.skipped?
Expand Down
41 changes: 41 additions & 0 deletions src/spectator/formatters/junit/failed_test_case.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "../../assertion_failed"
require "./test_case"

module Spectator::Formatters::JUnit
struct FailedTestCase < TestCase
def initialize(*,
name : String,
class_name : String,
assertions : Int32? = nil,
time : Time::Span? = nil,
file : String? = nil,
line : Int32? = nil,
@error : Exception)
super(name: name, class_name: class_name, assertions: assertions, time: time, file: file, line: line)
end

private def has_inner_content? : Bool
true
end

private def print_inner(io : IO, indent : Int) : Nil
error = @error
type = error.is_a?(AssertionFailed) ? "failure" : "error"
print_indent(io, indent)
io << '<' << type
write_xml_attribute(io, "message", error.message)
write_xml_attribute(io, "type", error.class.name)

if backtrace = error.backtrace?
io.puts '>'
HTML.escape(backtrace.join('\n'), io)
io.puts
print_indent(io, indent)
io << "</" << type
io.puts '>'
else
io.puts " />"
end
end
end
end
12 changes: 12 additions & 0 deletions src/spectator/formatters/junit/passed_test_case.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "./test_case"

module Spectator::Formatters::JUnit
struct PassedTestCase < TestCase
private def has_inner_content? : Bool
false
end

private def print_inner(io : IO, indent : Int) : Nil
end
end
end
27 changes: 27 additions & 0 deletions src/spectator/formatters/junit/skipped_test_case.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "./test_case"

module Spectator::Formatters::JUnit
struct SkippedTestCase < TestCase
def initialize(*,
name : String,
class_name : String,
assertions : Int32? = nil,
time : Time::Span? = nil,
file : String? = nil,
line : Int32? = nil,
@skip_message : String? = nil)
super(name: name, class_name: class_name, assertions: assertions, time: time, file: file, line: line)
end

private def has_inner_content? : Bool
true
end

private def print_inner(io : IO, indent : Int) : Nil
print_indent(io, indent)
io << "<skipped"
write_xml_attribute(io, "message", @skip_message)
io.puts " />"
end
end
end
32 changes: 8 additions & 24 deletions src/spectator/formatters/junit/test_case.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require "../../assertion_failed"
require "./xml_element"

module Spectator::Formatters::JUnit
struct TestCase
abstract struct TestCase
include XMLElement

def initialize(*,
Expand All @@ -11,8 +10,7 @@ module Spectator::Formatters::JUnit
@assertions : Int32? = nil,
@time : Time::Span? = nil,
@file : String? = nil,
@line : Int32? = nil,
@error : Exception? = nil)
@line : Int32? = nil)
end

def to_xml(io : IO, indent : Int = 0) : Nil
Expand All @@ -25,32 +23,18 @@ module Spectator::Formatters::JUnit
write_xml_attribute(io, "file", @file)
write_xml_attribute(io, "line", @line)

if error = @error
io.puts '>'
print_failure(io, error, indent + 1)
if has_inner_content?
io.puts ">"
print_inner(io, indent + 1)
print_indent(io, indent)
io << "</testcase>"
else
io << " />"
end
end

private def print_failure(io, error, indent) : Nil
type = error.is_a?(AssertionFailed) ? "failure" : "error"
print_indent(io, indent)
io << '<' << type
write_xml_attribute(io, "message", error.message)
write_xml_attribute(io, "type", error.class.name)
private abstract def has_inner_content? : Bool

if backtrace = error.backtrace?
io.puts '>'
HTML.escape(backtrace.join('\n'), io)
io.puts
print_indent(io, indent)
io << "</" << type
io.puts '>'
else
io.puts " />"
end
end
private abstract def print_inner(io : IO, indent : Int) : Nil
end
end

0 comments on commit 8a7042c

Please sign in to comment.