Encodes ExUnit test structures to JSON-serializable maps.
This module handles the conversion of ExUnit structs (tests, failures,
stacktraces) into plain maps that can be serialized to JSON using
Elixir's built-in :json module.
Summary
Functions
Encodes failure details from a failed test.
Encodes a stacktrace to a list of frame maps.
Encodes a test state to a string representation.
Encodes test tags, filtering out internal ExUnit keys.
Encodes an ExUnit.Test struct to a JSON-serializable map.
Types
@type encoded_test() :: %{ name: String.t(), module: String.t(), file: String.t() | nil, line: non_neg_integer() | nil, state: String.t(), duration_us: non_neg_integer(), tags: map(), failures: [map()] }
JSON-serializable test result map
@type test() :: %ExUnit.Test{ case: term(), logs: term(), module: term(), name: term(), parameters: term(), state: term(), tags: term(), time: term() }
An ExUnit test struct
@type test_state() :: nil | {:failed, list()} | {:skipped, binary()} | {:excluded, binary()} | {:invalid, module()}
Test state as returned by ExUnit.
nil- test passed{:failed, failures}- test failed with list of failure tuples{:skipped, message}- test was skipped{:excluded, message}- test was excluded by filters{:invalid, module}- test module is invalid
Functions
@spec encode_failure(test_state()) :: [map()]
Encodes failure details from a failed test.
Handles assertion errors specially to extract left/right values.
Encodes a stacktrace to a list of frame maps.
Each frame contains file, line, and optionally module, function, arity, and app.
@spec encode_state(test_state()) :: String.t()
Encodes a test state to a string representation.
State mappings
nil->"passed"{:failed, _}->"failed"{:skipped, _}->"skipped"{:excluded, _}->"excluded"{:invalid, _}->"invalid"
Encodes test tags, filtering out internal ExUnit keys.
Removes internal ExUnit keys and converts remaining tags to JSON-safe values.
Keys starting with :ex_ are also filtered as they are ExUnit internal.
@spec encode_test(test()) :: encoded_test()
Encodes an ExUnit.Test struct to a JSON-serializable map.
Examples
test = %ExUnit.Test{name: :"test example", state: nil, time: 1000}
encode_test(test)
%{name: "test example", state: "passed", duration_us: 1000, ...}