Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a test suite using Ruby minitest

I'm moving my tests to the new ruby minitest library, and I am looking for the class that corresponds to the old Test::Unit::TestSuite class. All the examples I've found online show single test cases, but I've got:

require 'minitest/unit/testsuite'
require 'minitest/unit/ui/console/testrunner'

require 'tests/fs_session_test'
require 'tests/resource_test'
require 'tests/rest_session_test'
require 'tests/server_test'

class AllTests
  def self.suite
    suite = Test::Unit::TestSuite.new
    suite << FSSessionTest.suite
    suite << ResourceTest.suite
    suite << RESTSessionTest.suite
    suite << ServerTest.suite
  end
end

Test::Unit::UI::Console::TestRunner.run(AllTests)

and I keep getting a LoadError on the testsuite require.

like image 873
spierepf Avatar asked Jul 03 '12 12:07

spierepf


People also ask

How do you run a Minitest test?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

What is Minitest in Ruby?

What is Minitest? Minitest is a testing tool for Ruby that provides a complete suite of testing facilities. It also supports behaviour-driven development, mocking and benchmarking. With the release of Ruby 1.9, it was added to Ruby's standard library, which increased its popularity.

What is the testing framework for Ruby?

test-unit (Test::Unit) is unit testing framework for Ruby, based on xUnit principles. These were originally designed by Kent Beck, creator of extreme programming software development methodology, for Smalltalk's SUnit. It allows writing tests, checking results and automated testing in Ruby.

What is Minitest in Ruby?

minitest doesn't reinvent anything that ruby already provides, like: classes, modules, inheritance, methods. This means you only have to learn ruby to use minitest and all of your regular OO practices like extract-method refactorings still apply. minitest/autorun - the easy and explicit way to run all your tests.

How readable is Minitest/unit?

I MUST say that minitest is *very* readable / understandable compared to the 'other two' options we looked at. Nicely done and thank you for helping us keep our mental sanity." -- Wayne E. Seguin minitest/unit is a small and incredibly fast unit testing framework. It provides a rich set of assertions to make your tests clean and readable.

What are some useful additions to Minitest?

Semantically symmetric aliases for assertions and expectations. Clean API for excluding certain tests you don't want to run under certain conditions. Makes your MiniTest mocks more resilient. Generally useful additions to minitest's assertions and expectations Test notifier for minitest via growl. Implicit declaration of the test subject.

What is the difference between Seguin Minitest/unit and Minitest / spec?

-- Wayne E. Seguin minitest/unit is a small and incredibly fast unit testing framework. It provides a rich set of assertions to make your tests clean and readable. minitest/spec is a functionally complete spec engine. It hooks onto minitest/unit and seamlessly bridges test assertions over to spec expectations.


1 Answers

There is no Test::Unit::TestSuite in minitest. You have several options, assuming your tests look something like this:

require 'minitest/unit'
require 'minitest/autorun'

class FSSessionTest < MiniTest::Unit::TestCase
  def test_the_truth
    assert true
  end
end

The vital part here is require 'minitest/autorun' which uses at_exit to run all tests it can find, just before the enclosing script exits. I find this to be the easiest way for running my test suites.

Run tests with Rake

For example, you can create a Rakefile using Rake::TestTask which runs all the tests in your test/ directory:

require 'rake'
require 'rake/testtask'

Rake::TestTask.new do |t|
  t.pattern = 'tests/**/*_test.rb'
end

Run the tests with

$ rake test

Require tests in a Ruby file

If you frequently only need certain tests, you can also write a test script, something like

require './tests/fs_session_test'
require './tests/resource_test'
require './tests/rest_session_test'
require './tests/server_test'

You could also include require 'minitest/autorun' at the top of this file to ensure, the tests are run, but i do this at the top of every test file, anyway. Run the suite with

$ ruby test.rb

Result

Both methods give you the same output, for example something like

Run options: --seed 5559

# Running tests:

....

Finished tests in 0.001909s, 2095.3379 tests/s, 2095.3379 assertions/s.

4 tests, 4 assertions, 0 failures, 0 errors, 0 skips

Because mintiest makes use of at_exit, there is really no need to group the tests before you run them. You never get the output of only one test. Unless, of course you run a test on its own, for example with

$ ruby tests/fs_session_test.rb 
Run options: --seed 43007

# Running tests:

.

Finished tests in 0.000672s, 1488.0952 tests/s, 1488.0952 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
like image 159
Patrick Oscity Avatar answered Oct 09 '22 16:10

Patrick Oscity