minitest.cr v0.5.0

Test Unit for the Crystal programming language

Minitest for Crystal

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

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
      meme.i_can_has_cheezburger?.must_equal("OHAI!")
    end
  end

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

You may use assertions in your specs (they'll work the same) or you may prefer the expect syntax:

expect(meme.i_can_haz_cheezeburger?).must_equal("OHAI!")
expect(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/

When using Minitest::Spec with assertions or the expect syntax, you can avoid to taint Object with all the #must_ and #wont_ expectations:

$ crystal run -Dmt_no_expectations spec/meme_spec.cr

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

CHANGELOG

v0.5.0

Requires:

  • Crystal >= 0.31.0 (Channel changes)

Fixes:

  • MT compatibility

v0.4.3

Fixes:

  • compatibility with recent Crystal releases (Exception initializer hack);

v0.4.2

Fixes:

  • --name regression since focus was introduced.

v0.4.1

Features:

  • Introduce minitest/focus to specify tests/specs to focus. Complements the --name argument and is easier to use in specs.

Fixes:

  • Local variable errors in responds_to assertions;
  • Compatibility with Crystal 0.27.0

v0.4.0

Features:

  • Add assert_instance_of, refute_instance_of assertions;
  • Add must_be_instance_of, wont_be_instance_of expectations;
  • Add capture_io, assert_silent, assert_output assertions.
  • Add --chaos to merge & shuffle all tests from all suites, instead of shuffling suites then shuffling tests for each suite;
  • Add --seed SEED for reproducible test runs (can also be set with SEED environment variable).

v0.3.6

Changes:

  • Measure elapsed time using monotonic clock.

Fixes:

  • Compatibility with Crystal 0.24.0

v0.3.5

Changes:

  • dropped artificial src/minitest namespace for files

Fixes:

  • Compatibility with Crystal 0.19.0

v0.3.4

Fixes:

  • Compatibility with Crystal 0.17.0

v0.3.3

Fixes:

  • Compatibility with Crystal > 0.15.0

v0.3.2

Fixes:

  • Compatibility with Crystal 0.14.0

v0.3.1

Fixes:

  • Compatibility with Crystal 0.11.0

v0.3.0

Breaking Change:

  • Each test now runs in a single instance of the test class, so instance variables don't leak from one test to another.

    This change will have a breaking impact if you rely on instance variables to cache or share data between tests. You'll may want to use class variables for this purpose instead.

Fixes:

  • Allow describes in specs to start with special chars like . or #
  • Allow skip :symbol and flunk :symbol

v0.2.0

Feature:

  • Minitest.after_run hooks

Fixes:

  • Crystal > 0.9.1 compatibility

v0.1.5

Fixes:

  • Don't exit until all test suites have completed
  • Exception overload with Crystal 0.8.0

v0.1.4

  • Compatibility with Crystal 0.8.0

v0.1.3

  • Compatibility with Crystal 0.7.7 (no more alias_method)

v0.1.2

  • Fixes verbose mode

v0.1.1

  • Added shard.yml for shards dependency manager

v0.1.0

  • Initial release: unit tests, specs, runner, ...
Github statistic:
  • 104
  • 58
  • 11
  • 7
  • 0
  • 3 days ago

License:

MIT License

Links: