Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up MiniTest?

I'm a fairly novice tester, but have been trying to get better at TDD in Rails.

RSpec works great, but my tests are pretty slow. I've heard that MiniTest is a lot faster, and the MiniTest/Spec DSL looks pretty similar to how I'm used to working with RSpec, so I thought I'd give it a try.

However, I have not been able to find anything on the web that provides a walkthrough of how to setup and run Minitest. I learned how to test from the RSpec book, and I have no idea how Test::Unit or MiniTest are supposed to work. I have the gem in my gemfile, I've written a few simple tests, but I have no idea where to put them or how to run them. I figure this is one of those things that's so obvious nobody has bothered to write it down...

Can anyone explain to me how to setup some some Minitest/spec files and get them running so I can compare the performance against Rspec?

EDIT

Specifically these are the basics I most need to know:

  1. Do you need a test_helper file (like spec_helper) and if so how do you create it?
  2. How do you run minitest? There doesn't seem to be an equivalent to rspec spec or rspec path/to/file_spec.rb, what am I missing?

Thanks!

like image 937
Andrew Avatar asked Jul 16 '11 03:07

Andrew


People also ask

How do you run a Minitest?

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.

Is Minitest part of Ruby?

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 Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.

How do I run test rails?

2.7 The Rails Test Runner Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.

What is the best way to do testing in Minitest?

Testing in Minitest basically follows several naming conventions. The /test directory should look like an /app directory, which means the same type of tests should be put together. Files should start with test, which means it should look like test_post.rb.

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. Even though at first it gives off the impression...

What is the best way to organize testing files in Minitest?

Testing in Minitest basically follows several naming conventions. The /test directory should look like an /app directory, which means the same type of tests should be put together. Files should start with test, which means it should look like test_post.rb. Class name should be suffixed by Test, and inherited from any Minitest class.

What should the test directory look like in Minitest?

The /test directory should look like an /app directory, which means the same type of tests should be put together. Files should start with test, which means it should look like test_post.rb. Class name should be suffixed by Test, and inherited from any Minitest class.


2 Answers

This question is similar to How to run all tests with minitest?

Using Ruby 1.9.3 and Rake 0.9.2.2, given a directory layout like this:

Rakefile lib/alpha.rb spec/alpha_spec.rb 

Here is what alpha_spec.rb might look like:

require 'minitest/spec' require 'minitest/autorun'  # arranges for minitest to run (in an exit handler, so it runs last)  require 'alpha'  describe 'Alpha' do   it 'greets you by name' do     Alpha.new.greet('Alice').must_equal('hello, Alice')   end end 

And here's Rakefile

require 'rake' require 'rake/testtask'  Rake::TestTask.new do |t|   t.pattern = 'spec/**/*_spec.rb' end 

You can run

  • all tests: rake test
  • one test: ruby -Ilib spec/alpha_spec.rb

I don't know if using a spec_helper.rb with minitest is common or not. There does not appear to be a convenience method for loading one. Add this to the Rakefile:

require 'rake' require 'rake/testtask'  Rake::TestTask.new do |t|   t.pattern = 'spec/**/*_spec.rb'   t.libs.push 'spec' end 

Then spec/spec_helper.rb can contain various redundant things:

require 'minitest/spec' require 'minitest/autorun' require 'alpha' 

And spec/alpha_spec.rb replaces the redundant parts with:

require 'spec_helper' 
  • all tests: rake test
  • one test: ruby -Ilib -Ispec spec/alpha_spec.rb
like image 109
Patrick Avatar answered Sep 29 '22 03:09

Patrick


Note that watchr or spork are not requirements for running tests. They're a convenience for doing autotesting. But the most basic way you can run a set of MiniTest tests is with ruby itself:

$ ruby myfileoftests.rb 
like image 31
cczona Avatar answered Sep 29 '22 05:09

cczona