ExUnitJSON.JSONEncoder (ex_unit_json v0.4.2)

Copy Markdown View Source

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

Types

JSON-serializable test result map

An ExUnit test struct

Test state as returned by ExUnit.

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

encoded_test()

@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

test()

@type test() :: %ExUnit.Test{
  case: term(),
  logs: term(),
  module: term(),
  name: term(),
  parameters: term(),
  state: term(),
  tags: term(),
  time: term()
}

An ExUnit test struct

test_state()

@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

encode_failure(arg1)

@spec encode_failure(test_state()) :: [map()]

Encodes failure details from a failed test.

Handles assertion errors specially to extract left/right values.

encode_stacktrace(stacktrace)

@spec encode_stacktrace(list()) :: [map()]

Encodes a stacktrace to a list of frame maps.

Each frame contains file, line, and optionally module, function, arity, and app.

encode_state(arg1)

@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"

encode_tags(tags)

@spec encode_tags(map()) :: map()

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.

encode_test(test)

@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, ...}