How to organize minitest/unit tests?
I know several folks coming from RSpec to minitest struggling with the same question. They love the ability to nest using describe/context blocks and want to continue in minitest. There are several solutions:
- Use minitest's spec DSL: While there are minor differences, the spec DSL gives you most (all?) of the good parts of the rspec DSL. The big difference is the lack of
context
blocks. But you can just as easily usedescribe
in its place and everything works as you'd expect. - Use directories and files: I prefer this option. I dislike scrolling through a 300 line test file, regardless whether its using the spec DSL or the classical xUnit style. I do not find nesting unrelated tests helpful. The same rules for comprehension for code applies to tests. So break it up. Create a directory and place several files within it.
Here is an example of how my test files are organized:
test/
models/
user/
authentication_test.rb
email_test.rb
reservation_test.rb
user_test.rb
username_test.rb
I use this structure whether I'm using the spec DSL or the xUnit style. When using the spec DSL I specify what I'm testing in my describe block like so:
require "minitest_helper"
describe User, :authentications do
before do
# ...
How to run MiniTest::Unit tests in ordered sequence?
You can use i_suck_and_my_tests_are_order_dependent!()
class method.
class MyTest < MiniTest::Unit::TestCase
i_suck_and_my_tests_are_order_dependent! # <----
def test_1
p 1
end
def test_2
p 2
end
end
But as the name suggest, it's not a good idea to make the tests to be dependant on orders.
How do you nest or group Test::Unit tests?
Test::Unit
, to my knowledge, does not support test contexts. However, the gem contest
adds support for context blocks.
How can you have nested tests with Minitest?
Yes you can. You can do something like this (not the prettiest):
class Person < ActiveSupport::TestCase
class MovementMethods < ActiveSupport::TestCase
test "#run" do
# something
end
test "#jump" do
# something
end
end
end
Also consider using minitest/spec and you can write your tests cases more comparable to the Jasmine snippet:
require 'minitest/spec'
describe Person do
describe 'movement methods' do
it '#run' do
# something
end
it '#jump' do
# something
end
end
end
Related Topics
How to View a Sample of the Call Stack in Ruby
Rails 3 Joins -- Select Only Certain Columns
Using Yield Inside Define_Method in Ruby
What Do the Fields of Ruby's Gc.Stat Mean
I'm Getting "Found Character That Cannot Start Any Token While Scanning for the Next Token"
Jekyll: How to Use Custom Plugins with Github Pages
How to Get a HTML Table Row with Capybara
Rails 3 Validates Inclusion of When Using a Find (How to Proc or Lambda)
How to Debug Ruby on Rails in Eclipse Aptana Plugin
How to Trace and Check Dependencies in Bundled Ruby Gems
Rspec Stubbing Method for Only Specific Arguments
Rendering File with Mime Type in Rails
What's the Difference Between Arrays and Hashes
Find Memory Leak in a Ruby on Rails Project
Strong Parameters Require Multiple