Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

187 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minitest for Crystal

Unit tests and assertions for the Crystal programming language, using the fantastic minitest as reference.

The minitest rationale brings a number of benefits over stdlib spec:

  • Test suites (and describe blocks) are simple classes. Build a test suite hierarchy, add methods and include modules without polluting the global namespace, and don't impact other test suites.
  • One module per suite, not everything in a single module (faster codegen).
  • Simple assertions and expect expectations.

Increase the trust in your program's correctness:

  • Test suites run concurrently to each others.
  • Test suites run in parallel to each others.
  • Tests run in random order by default — use seed for reproducible runs.
  • Chaos mode to flatten tests and randomize everything (full random, full concurrent, full parallel).

Getting Started

Given that you'd like to test the following class:

class Meme
  def i_can_has_cheezburger?
    "OHAI!"
  end

  def will_it_blend?
    "YES!"
  end
end

Unit Tests

Define your tests as methods beginning with test_:

require "minitest/autorun"

class MemeTest < Minitest::Test
  def meme
    @meme ||= Meme.new
  end

  def test_that_kitty_can_eat
    assert_equal "OHAI!", meme.i_can_has_cheezburger?
  end

  def test_that_it_will_not_blend?
    refute_match /^no/i, meme.will_it_blend?
  end

  def test_that_will_be_skipped
    skip "test this later"
  end
end

Specs

Specs follow the same design rationale as the original Minitest: describe generates classes that inherit from Minitest::Spec, and it generates test methods.

require "minitest/autorun"

describe Meme do
  let(:meme) { Meme.new }

  describe "when asked about cheeseburgers" do
    it "must respond positively" do
      expect(meme.i_can_has_cheezburger?).must_equal("OHAI!")
    end
  end

  describe "when asked about blending possibilities" do
    it "won't say no" do
      expect(meme.will_it_blend?).wont_match(/^no/i)
    end
  end
end

You may use assertions in your specs (they're the same) or you may prefer to skip the expect syntax at the expense of polluting Object (legacy, discouraged):

class Object
  include Minitest::Expectations
end

meme.i_can_haz_cheezeburger?.must_equal("OHAI!")
meme.will_it_blend?.wont_match(/^no/i)

Run Tests

Eventually run the tests:

$ crystal run test/meme_test.cr spec/meme_spec.cr -- --verbose

You may filter your tests using an exact test name, or a regexp:

$ crystal run test/meme_test.cr -- -n test_that_kitty_can_eat
$ crystal run test/meme_test.cr -- -n /will/

License

Distributed under the MIT License. Please see LICENSE for details.

Credits

  • Julien Portalier @ysbaddaden for the Crystal implementation
  • Ryan Davis @zenspider and seattle.rb for the original Ruby gem

About

Test Unit for the Crystal programming language

Topics

Resources

Stars

153 stars

Watchers

7 watching

Forks

Releases

Used by

Contributors

Languages