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.
在多个项目中使用RSpec之后,我将尝试使用minitest/unit a。到目前为止,我很喜欢它,但是我没有使用描述/上下文块以一种合乎逻辑的方式对我的测试/规范进行分组。
I know minitest/spec provides this functionality, but I like that minitest/unit feels a bit closer to barebones Ruby.
我知道minitest/spec提供了这个功能,但我喜欢这种最小/单元感觉更接近于赤膊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?
是否有提供描述/上下文支持的宝石/单元?或者,我是否应该和我的长期、无组织的测试文件一起生活在minitest/unit中?
3 个解决方案
#1
41
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:
我知道有些人从RSpec到minitest都在为同一个问题而挣扎。他们喜欢用描述/上下文块来嵌套的能力,并且希望继续在minitest中进行。有几种解决方案:
- 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. - 使用minitest的spec DSL:虽然有细微的差异,但是spec DSL提供了rspec DSL的大部分(全部)优点。最大的区别在于缺少上下文块。但是你也可以很容易地使用description in its place,一切都可以按照你的预期工作。
- 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.
- 使用目录和文件:我更喜欢这个选项。我不喜欢滚动一个300行的测试文件,不管它是使用spec DSL还是传统的xUnit样式。我不认为嵌套不相关的测试有用。同样的代码理解规则也适用于测试。所以分解。创建一个目录并在其中放置几个文件。
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:
无论使用spec DSL还是xUnit样式,我都使用这种结构。在使用spec DSL时,我在描述块中指定要测试的内容如下:
require "minitest_helper"
describe User, :authentications do
before do
# ...
#2
12
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:
甚至是nest的测试类:
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
10
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
#1
41
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:
我知道有些人从RSpec到minitest都在为同一个问题而挣扎。他们喜欢用描述/上下文块来嵌套的能力,并且希望继续在minitest中进行。有几种解决方案:
- 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. - 使用minitest的spec DSL:虽然有细微的差异,但是spec DSL提供了rspec DSL的大部分(全部)优点。最大的区别在于缺少上下文块。但是你也可以很容易地使用description in its place,一切都可以按照你的预期工作。
- 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.
- 使用目录和文件:我更喜欢这个选项。我不喜欢滚动一个300行的测试文件,不管它是使用spec DSL还是传统的xUnit样式。我不认为嵌套不相关的测试有用。同样的代码理解规则也适用于测试。所以分解。创建一个目录并在其中放置几个文件。
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:
无论使用spec DSL还是xUnit样式,我都使用这种结构。在使用spec DSL时,我在描述块中指定要测试的内容如下:
require "minitest_helper"
describe User, :authentications do
before do
# ...
#2
12
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:
甚至是nest的测试类:
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
10
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