How to organize minitest/unit tests?

独自空忆成欢 提交于 2019-12-04 14:57:24

问题


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?


回答1:


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
    # ...



回答2:


You can also throw multiple classes into one test file:

module PizzaTest
  class Isolation < ActiveSupport::TestCase
    test "is awesome by default" do
      assert Pizza.new.awesome?
    end
  end

  class Integration < ActiveSupport::TestCase
    fixtures :all

    test "is awesome too" do
      pizzas('one-with-everything').awesome?
    end
  end
end

and even nest test classes:

class PizzaTest < ActiveSupport::TestCase
  test "is awesome by default" do
    assert Pizza.new.awesome?
  end

  class Integration < ActiveSupport::TestCase
    fixtures :all

    test "is awesome too" do
      assert pizzas('one-with-everything').awesome?
    end
  end
end



回答3:


I prefer this way (only a little bit) but I think it easier to follow:

class ConventionalNameTest < ActiveSupport::TestCase
  class ContextTest < ConventionalNameTest
    # so much stuff...
  end
  class AnotherContextTest < ConventionalNameTest
    # and some more...
  end


来源:https://stackoverflow.com/questions/14263839/how-to-organize-minitest-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!