dream_test/reporters/json
JSON test reporter for dream_test.
This reporter outputs test results as JSON for CI/CD integration, test aggregation, and tooling.
The JSON object includes:
version: schema versiontimestamp_ms: when the report was createdduration_ms: total duration (sum of test durations)system:os,otp_version,gleam_versionsummary: counts by statustests: per-test details (name, full_name, status, duration_ms, tags, kind, failures)
Usage
import dream_test/matchers.{succeed}
import dream_test/reporters/json
import dream_test/reporters/progress
import dream_test/runner
import dream_test/unit.{describe, it}
pub fn tests() {
describe("JSON Reporter", [
it("outputs JSON format", fn() {
// The json reporter prints machine-readable JSON at the end of the run.
Ok(succeed())
}),
it("includes test metadata", fn() {
// JSON output includes name, full_name, status, duration, tags
Ok(succeed())
}),
])
}
pub fn main() {
runner.new([tests()])
|> runner.progress_reporter(progress.new())
|> runner.results_reporters([json.new()])
|> runner.exit_on_failure()
|> runner.run()
}
Values
pub fn format(results results: List(types.TestResult)) -> String
Format test results as a compact JSON string.
Returns a single-line JSON string suitable for machine parsing.
Parameters
results: The test results to encode into the JSON report
Returns
A compact (single-line) JSON string.
Example
import dream_test/matchers.{succeed}
import dream_test/reporters/json
import dream_test/runner
import dream_test/unit.{describe, it}
fn example_suite() {
describe("Example Suite", [
it("passes", fn() { Ok(succeed()) }),
])
}
pub fn main() {
let results = runner.new([example_suite()]) |> runner.run()
json.format(results)
}
pub fn format_pretty(
results results: List(types.TestResult),
) -> String
Format test results as pretty-printed JSON.
Returns an indented, human-readable JSON string with 2-space indentation.
Parameters
results: The test results to encode into the JSON report
Returns
A pretty-printed JSON string with 2-space indentation.
Example
import dream_test/matchers.{succeed}
import dream_test/reporters/json
import dream_test/runner
import dream_test/unit.{describe, it}
fn example_suite() {
describe("Example Suite", [
it("passes", fn() { Ok(succeed()) }),
])
}
pub fn main() {
let results = runner.new([example_suite()]) |> runner.run()
json.format_pretty(results)
}
pub fn new() -> types.ResultsReporter
Create a JSON results reporter (printed at the end of the run).
pub fn pretty(
reporter reporter: types.ResultsReporter,
) -> types.ResultsReporter
Enable pretty-printed JSON output.
pub fn render(
config config: types.JsonReporterConfig,
results results: List(types.TestResult),
) -> String
Render JSON output for a completed run.
pub fn report(
results results: List(types.TestResult),
write write: fn(String) -> Nil,
) -> List(types.TestResult)
Print test results as JSON using a provided writer function.
This is the main entry point for JSON reporting. The writer function receives the JSON string and can print it, log it, or write it to a file.
Parameters
results: The test results to encode and writewrite: Output sink for the JSON string (for exampleio.print)
Returns
Returns the input results unchanged, enabling pipeline composition.
Example
import dream_test/matchers.{succeed}
import dream_test/reporters/json
import dream_test/runner
import dream_test/unit.{describe, it}
import gleam/io
fn example_suite() {
describe("Example Suite", [
it("passes", fn() { Ok(succeed()) }),
])
}
pub fn main() {
let results = runner.new([example_suite()]) |> runner.run()
results |> json.report(io.print)
}
pub fn report_pretty(
results results: List(types.TestResult),
write write: fn(String) -> Nil,
) -> List(types.TestResult)
Print test results as pretty-printed JSON using a provided writer function.
Same as report but with indented, human-readable output.
Parameters
results: The test results to encode and writewrite: Output sink for the JSON string (for exampleio.print)
Returns
Returns the input results unchanged, enabling pipeline composition.
Example
import dream_test/matchers.{succeed}
import dream_test/reporters/json
import dream_test/runner
import dream_test/unit.{describe, it}
import gleam/io
fn example_suite() {
describe("Example Suite", [
it("passes", fn() { Ok(succeed()) }),
])
}
pub fn main() {
let results = runner.new([example_suite()]) |> runner.run()
results |> json.report_pretty(io.print)
}