🔬 microtest Crystal CI

Version 2.0.0

A small testing framework inspired by minitest/minitest.cr.

See CHANGELOG.md for changes and compatibility notes.

Features

Installation

Add this to your application's shard.yml:

development_dependencies:
  microtest:
    github: ragmaanir/microtest
    version: ~> 2.0.0

And add this to your spec_helper.cr:

require "microtest"

include Microtest::DSL

Microtest.run!

Usage

class WaterPump
  getter name : String
  getter speed : Int32
  getter? enabled : Bool = false

  def initialize(@name, @speed = 10)
  end

  def enable
    @enabled = true
  end
end

describe WaterPump do
  test "enabling" do
    p = WaterPump.new("main")
    p.enable

    assert(p.enabled?)
  end

  test "pump speed" do
    p = WaterPump.new("main", speed: 100)

    assert(p.speed > 50)
  end

  test "this one is pending since it got no body"

  pending "this one is pending even though it has a body" do
    raise "should not raise"
  end
end

Run the test with:

crystal spec

You can provide the seed to run the tests in the same order:

SEED=123 crystal spec

In PowerShell, use $env:SEED = "123"; crystal spec. Seeds are unsigned 32-bit integers. Microtest.run! exits with a nonzero status on failures, hook errors, or manual interruption; Microtest.run returns a Boolean instead of exiting.

Assertions

assert(expression) passes for a truthy result; false and nil fail. Call arguments, named arguments, keyword splats, and forwarded procs are supported. Values are evaluated once, and diagnostics are generated when an assertion fails. If inspecting a value raises, the diagnostic contains an <inspection failed: ...> placeholder rather than hiding the assertion failure.

Blocks execute normally. Power assertions describe the enclosing call, not the individual expressions inside its block. An exception raised while evaluating the asserted expression remains an exception from the test body.

Use assert_raises to check exceptions:

describe Parsing do
  test "rejects invalid integers" do
    assert_raises(ArgumentError) { "not an integer".to_i }
  end
end

assert_raises accepts the expected exception type and optional file : String and line : Int32 arguments. It fails if the block raises a different exception or does not raise.

Hooks and cleanup

Declare one before, after, and around hook at most in each describe class. Global hooks use Microtest.before, Microtest.after, and Microtest.around, also with one declaration of each kind. Global and local hooks compose; a second hook of the same kind in the same scope is a compile-time error.

describe Collection do
  before do
    @values = [1, 2]
  end

  after do
    # Setup may have failed before assigning @values.
    @values.try(&.clear)
  end

  test "starts with two values" do
    assert @values.not_nil!.size == 2
  end
end

Before hooks run from global/base to local/derived; after hooks run in reverse. Around hooks wrap that whole sequence, with the global/base wrapper outermost. An around hook should invoke its block exactly once. A wrapper that does not invoke it aborts the run; multiple invocations are not currently rejected. Use ensure for cleanup owned by an around wrapper:

describe Wrapped do
  around do |block|
    begin
      block.call
    ensure
      # Release resources acquired by this wrapper here.
    end
  end

  test "runs inside the wrapper" do
    assert true
  end
end

Once setup is attempted, after hooks run even if setup fails. A failed setup skips the test body. Each DSL-defined after hook attempts its inherited cleanup even if its own body raises. Make cleanup safe for partially initialized state. If you override after_hooks directly, you are responsible for calling super and preserving errors from both implementations.

Test-body failures are recorded and the suite continues. Hook errors abort the remaining run. A single cleanup error retains its original exception object; multiple errors are collected in Microtest::CleanupErrors#exceptions in cleanup order. A setup error remains primary, with cleanup errors in AbortionInfo#cleanup_exception. Both terminal and JSON reporters show the original errors and backtraces. Exceptions raised by arbitrary around-hook code follow normal Crystal exception semantics; use care when raising from ensure.

Power Assert Output

describe AssertionFailure do
  test "assertion failure" do
    a = 5
    b = "aaaaaa"
    assert "a" * a == b
  end
end

Generates:

missing

Microtest Test Output (microtest tests using progress reporter)

missing

Reporters

Use common reporter combinations:

# both versions include error-list-, slow-tests- and summary-reporters:
Microtest.run!(:progress)
Microtest.run!(:descriptions)

Or select the used reporters explicitly:

Microtest.run!([
  Microtest::DescriptionReporter.new,
  Microtest::ErrorListReporter.new,
  Microtest::SlowTestsReporter.new,
  Microtest::SummaryReporter.new,
] of Microtest::Reporter)
describe First do
  test "success" do
  end

  test "skip this"
end

describe Second do
  def raise_an_error
    raise "Oh, this is wrong"
  end

  test "first failure" do
    a = 5
    b = 7
    assert a == b * 2
  end

  test "error" do
    raise_an_error
  end
end

Progress Reporter

missing

Description Reporter

missing

When focus active

describe Focus do
  test "not focused" do
  end

  test! "focused" do
  end

  test "focused too", :focus do
  end
end

missing

JSON reports

Select only the JSON reporter to write one JSON summary to standard output:

Microtest.run!([Microtest::JsonSummaryReporter.new] of Microtest::Reporter)

Avoid writing other output to standard output when another program consumes this report. using_focus describes whether the run selected focused tests. The results object is keyed by suite and sanitized test name, for example CollectionTest#starts_with_two_values.

Always check top-level success, not just failure_count. A passing body can produce a TestSuccess result while its after hook aborts the run. A failed setup produces TestAbortion. Manual interruption is exposed as manually_aborted.

| Field | Meaning | | --- | --- | | abortion | null unless a hook error aborted the run | | abortion.message, abortion.backtrace | Primary hook error, or a summary when multiple cleanup hooks failed | | abortion.exceptions | Original error records for a primary cleanup-error group; otherwise null | | abortion.cleanup_exception | Additional cleanup failure after a primary setup/hook error; otherwise null | | abortion.cleanup_exception.exceptions | Original error records when that additional failure is a cleanup-error group; absent for a single error |

Each original error record has message, class, and backtrace. Consumers should tolerate additional fields and missing backtraces.

Development

Run crystal spec and crystal tool format --check. Use Ameba for optional linting.

Edit README.md.ecr, then run ./cli readme to regenerate the README and example images. Python and Rich render terminal output as SVG inside Docker.

Releasing

  1. Update the version in shard.yml and the changelog manually.
  2. Confirm CI passes and review the output of ./cli readme.
  3. Check the Git remotes, including gh, then run ./cli release.

The release command reruns tests, regenerates the README, prompts for review, then commits, tags, and pushes the release.