Test reports a pipeline can read
The test helper
The console output of a Ruby test run is readable by a person and useless to a pipeline. Most CI systems understand one format: JUnit XML, one report per test class. Minitest writes it with a reporter, and the reporter goes into one helper file that every test requires.
Add the reporter to the Gemfile
source "https://rubygems.org" gem "minitest", "~> 5.20" gem "minitest-reporters", "~> 1.6" gem "rake", "~> 13.0"
test/test_helper.rb
require "minitest/autorun" require "minitest/reporters" Minitest::Reporters.use! Minitest::Reporters::JUnitReporter.new
Then make the test file load the helper before anything else:
require_relative "test_helper" require_relative "../lib/todo" class TodoTest < Minitest::Test # the same three tests as before end
Run with the reporter
Install the new gem with bundle install once, then run one test file directly. The reporter replaces the dots with its own two-line notice:
bundle exec ruby -Itest test/todo_test.rb
Emptying /tmp/ci-demo/test/reports
Writing XML reports to /tmp/ci-demo/test/reports
The path in the output is the project folder on the machine that ran it; on yours it is your own folder. A dot in a plain Minitest run is one test, not one file, and the three tests of one class write one report.
The XML it writes
The file lands in test/reports/, one per class. This is the whole content for three passing tests, trimmed to the first test case:
<?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="TodoTest" filepath="test/todo_test.rb" skipped="0" failures="0" errors="0" tests="3" assertions="4"> <testcase name="test_done_marks_an_item" lineno="22" classname="TodoTest" assertions="1" file="test/todo_test.rb"> </testcase> <!-- two more testcase elements --> </testsuite> </testsuites>
A failed assertion adds a failure element with the message and the backtrace. The pipeline reads that element and shows the failing test by name, which is why the format is worth the extra gem.
Keep it as an artifact
A cache carries work forward; an artifact carries a result out. The report is an artifact, and both systems ask for the path:
- name: Keep the test report uses: actions/upload-artifact@v4 if: always() with: name: test-reports path: test/reports/*.xml
artifacts: when: always paths: - test/reports/*.xml
The always() and when: always matter: a failed job is exactly when someone wants the report, and the default keeps artifacts only for a successful run.
What to watch for
The artifact path must match the reporter output path. The reporter
prints the folder it uses on every run, so read that line once and copy
it into the CI file.
A JUnit file also survives a truncated log: the console output keeps the
first lines of a long failure, the XML keeps every test case. When a job
log is too long to scroll, the report is the faster answer.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why convert the console output of a test run into JUnit XML?
Answer
Most CI systems read the format and show each failing test by name
The log is for a person and the report is for the pipeline, which turns the test cases into a list it can display.
-
What does this program print?
rows = ["a", "b"] xml = rows.map { |r| "<testcase name=\"#{r}\"/>" }.join puts "<testsuite>#{xml}</testsuite>"Answer
<testsuite><testcase name="a"/><testcase name="b"/></testsuite>
Each row becomes one element and join puts them next to each other inside the suite element.
-
Why is the test report an artifact and not a cache?
Answer
It travels from the run to the person who reads the result, while a cache only saves time on the next run
The report is the result of this run; the cache is an optimisation that may be dropped at any time.
-
What does this program print?
require "cgi" puts CGI.escapeHTML("<a>")Answer
<a>
A test name with angle brackets would break the XML, so the reporter escapes the text before it writes the element.
-
What does this program print?
xml = "<testcase/><testcase/>" p xml.scan("<testcase").sizeAnswer
2
scan returns one match per opening tag, which is a quick way to count the cases of a report.
-
Why does the upload step use
if: always()orwhen: always?Answer
A failed job is exactly when the report is wanted, and the default uploads only after a success
Without the condition a red job loses the file that explains the failure.
-
What does this program print?
line = %q{<testsuite tests="3" failures="0">} p line[/tests="(\d+)"/, 1]Answer
"3"
The capture group keeps the digits without the quotes, and p shows the string with its quotes.
-
What does this program print?
failures = 1 puts failures.zero? ? "green" : "red"Answer
red
A non-zero failure count is the red case, which is the single fact a pipeline needs from the summary.
-
How does the reporter know where to write the report?
Answer
The reporter prints the folder it uses on every run, and the artifact path has to match it
The reporter has a default folder under the project, and the line it prints is the one to copy into the pipeline file.
-
What does this program print?
rows = [{ name: "a", assertions: 1 }, { name: "b", assertions: 2 }] puts rows.sum { |r| r[:assertions] }Answer
3
sum adds what the block returns for each row, which is how the assertion total of a suite is counted.
Article author: Arthur Isaev