检测 ruby 测试方法的覆盖
这样的测试类,
class MyTest < Test::Unit::TestCase
def setup
end
def test_1
flunk
end
def test_1
assert true
end
end
如果您编写像第一个 test_1 则会被忽略。 尽管这看起来像是一个愚蠢的错误,但复制和粘贴编程时可能会发生这种情况。 除了运行
grep test test_me.rb | wc
并比较测试单元所说的已运行的测试数量,或使用 rcov 或 heckle,或使用 -w 运行之外,您如何检测此类问题?
另外,有什么方法可以指定测试方法不应被覆盖吗?
编辑:正在测试的方法有一个大约有 6 个可能值的参数,测试人员想要测试每个场景。 这就是使用复制和粘贴编程的原因。 对于这种情况,我可以设想的唯一替代方案是参数和期望值的六元素数组。
If you write a test class like
class MyTest < Test::Unit::TestCase
def setup
end
def test_1
flunk
end
def test_1
assert true
end
end
the first test_1 is ignored. Although it looks like a stupid mistake, it can happen with copy and paste programming. Apart from running
grep test test_me.rb | wc
and comparing that with how many tests test unit says has run, or using rcov or heckle, or running with -w, how can you detect such issues?
Also, is there any way of specifying that test methods shouldn't be overwritten?
Edit: The method being tested had a parameter with 6 or so possible values, and the tester wanted to test each scenario. This was why copy and paste programming was used. The only alternative I can envisage for such a scenario is a a six-element array of parameters and expected values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以利用 Ruby 的
method_added
,只要将方法添加到类中,它就会被调用。 您应该能够将某些内容添加到您包含的模块中,但这里有一个在测试类中执行此操作的简单示例。You can take advantage of Ruby's
method_added
that gets called anytime a method is added to a class. You should be able to can something into a module that you include, but here is a simple example of doing it inside your test class.在这种情况下,我这样做:
这完全避免了复制和粘贴,正如已经说过的那样,这是不好的
确切地!
In those circumstances I do this:
This completely avoids copy and paste, which as has been said, is bad
Exactly!
关于 HermanD 的回答,因为这是 Ruby!,您也可以直接在类中执行此操作以创建独特的测试方法:
使用 Rspec 的(或 Shoulda 的)类似句子的语言感觉更自然:
Regarding HermanD's answer, since this is Ruby!, you can also do this directly in the class to create unique test methods:
It feels even more natural using Rspec's (or Shoulda's) sentence like language:
如果您为测试提供正确的描述性名称,这真的是一个问题吗? 以下是我最近的项目中一些测试方法名称的示例:
如果您的测试名称足够短,您可以轻松地覆盖或复制它们,那么您可能没有足够描述每个测试名称中实际测试的内容。
Is this really a problem if you're giving your tests proper descriptive names? Here's an example of some test method names from my most recent project:
If your test names are short enough that you can easily overwrite or duplicate them, you're probably not being descriptive enough as to what you're actually testing in each one.
避免复制粘贴编程。 如果需要,请重新绑定快捷键。
Avoid Copy-Paste programming. Rebind the key shortcuts if you have to.
从 2.0.7< 开始,测试单元的 gem 版本能够检测测试重新定义/a>.
The gem version of test-unit has the ability to detect test redefining as of 2.0.7.