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:
rspec spec
or rspec path/to/file_spec.rb
, what am I missing?Thanks!
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.
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? 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.
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.
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.
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...
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.
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.
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
rake 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'
rake test
ruby -Ilib -Ispec spec/alpha_spec.rb
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With