Testing with Minitest

Contents
Your first assertion
ruby -Itest
Describe blocks
Red green refactor

Your first assertion

Minitest ships with Ruby tooling as the default small test framework. A test class inherits from Minitest::Test and every method starting with test_ runs as one test.

Install it first with the steps in Gems and Bundler.

require "minitest/autorun" class TestMath < Minitest::Test def test_addition assert_equal 4, 2 + 2 end end

ruby -Itest

Test files usually live in a test folder with project code loaded beside them. The minus capital I flag adds that folder to the load path, so require finds project files.

Run one file directly with ruby while developing. Full suites usually run through rake later.

ruby -Itest test/test_math.rb

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Describe blocks

describe groups tests under a readable label, and it blocks name single behaviors. Readers see sentences instead of method names.

Assertions work the same inside, so teams mix both styles in one suite.

require "minitest/autorun" describe "calculator" do it "adds two numbers" do assert_equal 4, 2 + 2 end end

Red green refactor

Write a failing test first and watch it go red. Then write the smallest code that turns it green.

Refactor with green tests guarding you: clean the code without changing behavior, and rerun after each step.

def double(n) n * 2 end

Next: Debugging

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: