如何组织最小测试/单元测试? [英] How to organize minitest/unit tests?

查看:318
本文介绍了如何组织最小测试/单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将RSpec用于多个项目之后,我开始尝试minitest/unit.到目前为止,我很喜欢它,但是我想念使用describe/context块以逻辑方式对测试/规范进行分组.

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.

我知道minitest/spec提供了此功能,但是我喜欢minitest/unit感觉更接近Ruby准系统.

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

是否有任何宝石为minitest/unit提供描述/上下文支持?或者,我应该将我的长而无序的测试文件放在minitest/unit中吗?

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?

推荐答案

我知道有几位来自RSpec的人最小化了对同一问题的斗争.他们喜欢使用describe/context块嵌套的功能,并希望继续进行minitest.有几种解决方案:

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. 使用minitest的规格DSL:虽然有一些细微的差异,但规格DSL为您提供了rspec DSL的大部分(全部?).最大的区别是缺少context块.但是您可以轻松地在其位置使用describe,并且一切都会按预期进行.
  2. 使用目录和文件:我更喜欢此选项.我不喜欢滚动浏览300行的测试文件,无论它使用的是DSL规范还是经典的xUnit风格.我发现嵌套无关的测试没有帮助.相同的代码理解规则适用于测试.所以分手吧.创建目录并在其中放置几个​​文件.
  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

无论我使用规范DSL还是xUnit样式,我都使用此结构.使用规范DSL时,我在我的describe块中指定要测试的内容,如下所示:

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

这篇关于如何组织最小测试/单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆