Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize minitest/unit tests?

After using RSpec for several projects, I'm giving minitest/unit a go. I'm liking it so far, but I miss using describe/context blocks to group my tests/specs in a logical way.

I know minitest/spec provides this functionality, but I like that minitest/unit feels a bit closer to barebones Ruby.

Are there any gems that provide describe/context support for minitest/unit? Or, should I just live with my long, unorganized test files in minitest/unit?

like image 590
dojosto Avatar asked Jan 10 '13 17:01

dojosto


People also ask

How do you run a Minitest?

Setting Up 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.

How to run Rails test cases?

We can run all of our tests at once by using the bin/rails test command. 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.

How do I run a Ruby test file?

If your tests don't require any specific actions before start and you don't want to configure additional options, such as code coverage, you can run them by using the following options: Place the caret at the test class to run all tests in that class, or at the test method, and press Ctrl+Shift+F10 .

What is unit testing Rails?

They make sure that a section of an application, or a “unit”, is behaving as intended. In a Rails context, unit tests are what you use to test your models. Although it is possible in Rails to run all tests simultaneously, each unit test case should be tested independently to isolate issues that may arise.


1 Answers

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:

  1. 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 use describe in its place and everything works as you'd expect.
  2. 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     # ... 
like image 189
blowmage Avatar answered Sep 21 '22 18:09

blowmage