Ruby Logo

Minitest

Master the built-in test framework powering this project, covering unit tests, integration tests, and benchmarks.

Home Ruby Minitest

Minitest in Rails

Rails ships with Minitest and RubyDev embraces it for fast, batteries-included testing. The framework provides assertions, spec-style syntax, benchmarks, and parallel test execution without extra gems.

Test directories & helpers

Unit tests

`test/models`, `test/services`, `test/lib`. Require `test_helper`, leverage fixtures or factories, and assert small units of behaviour.

Integration & system

`test/integration` (request-style) and `test/system` (Capybara). Use `sign_in_as`, `visit`, and `assert_selector` helpers to simulate real flows.

Benchmarks

`test/performance` uses `Minitest::Benchmark`. Benchmark hot code paths (e.g., report generation) to prevent regressions.

Assertions and spec-style DSL

require "test_helper" class UserProgressTest < ActiveSupport::TestCase setup do @user = users(:demo) end test "calculates completion percentage" do progress = UserProgress.new(user: @user) assert_in_delta 0.6, progress.completion_ratio, 0.01 end test "raises when lesson missing" do assert_raises(ActiveRecord::RecordNotFound) do UserProgress.new(user: @user, lesson_id: 999).completion_ratio end end end

Need RSpec-like syntax? Include `require "minitest/spec"` and define `describe`/`it` blocks while still using Minitest assertions.

Harness parallelization safely

Rails runs tests in parallel by default (`parallelize(workers: :number_of_processors)`). Ensure stateful resources (files, Redis) are isolated per worker.

  • Wrap global configuration in `parallelize_setup` / `parallelize_teardown` to avoid race conditions.
  • Use `with_lock_retries` around database writes that might clash between workers.
  • Tag brittle suites with `self.use_transactional_tests = true` to reset data instantly.

Everyday workflow

  1. Run focused suites with `bin/rails test test/services --seed 12345` to reproduce order-dependent failures.
  2. Leverage fixtures in `test/fixtures/*.yml` for canonical data; supplement with FactoryBot when relationships get complex.
  3. Use `assert_enqueued_with` and `assert_performed_with` for ActiveJob assertions when verifying Sidekiq jobs.
  4. Capture regressions via `assert_changes`/`assert_no_changes` when validating state transitions.

Quick Navigation

Related Topics

Video Tutorial

Watch and learn minitest

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!