在 Ruby 中,如何控制 Test::Unit 测试的运行顺序?
例如,当运行这些测试时,我想确保 test_fizz
始终首先运行。
require 'test/unit'
class FooTest < Test::Unit::TestCase
def test_fizz
puts "Running fizz"
assert true
end
def test_bar
puts "Running bar"
assert true
end
end
更新:我为什么要这样做?我的想法是,某些测试(那些测试更简单、更基本的方法的测试)的早期失败将使追踪系统中的问题变得更容易。例如,bar
的成功取决于 fizz
的正常工作。如果 fizz
坏了,我想立即知道,因为不需要担心 bar
,它也会失败,但输出要复杂得多测试结果。
For example, when these tests are run, I want to ensure that test_fizz
always runs first.
require 'test/unit'
class FooTest < Test::Unit::TestCase
def test_fizz
puts "Running fizz"
assert true
end
def test_bar
puts "Running bar"
assert true
end
end
Update: Why do I want to do this? My thought is that early failure by certain tests (those testing the simpler, more fundamental methods) will make it easier to track down problems in the system. For example, the success of bar
hinges on fizz
working correctly. If fizz
is broken, I want to know that right off the bat, because there's no need to worry about bar
, which will fail too, but with much more complicated output in the test results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
同一测试类中的测试按照定义的顺序进行调用。但是,测试类是按类名的字母顺序运行的。
如果您确实需要精细控制,请使用
test_
之外的前缀定义 fizz 和 bar 方法,并从test_fizz_bar
方法内部按顺序调用它们,并在成功时有条件地运行 bar奔跑的嘶嘶声。编辑:
似乎不同的单元测试框架的行为不同。对于 Eclipse 中的 JUnit,测试用例似乎以随机顺序运行: 排序单元在 Eclipse 的 JUnit 视图中进行测试
Tests within the same test class are called in the order they are defined. However, test classes are run in alphabetical order by classname.
If you really need fine control, define the fizz and bar methods with a prefix other than
test_
and from inside atest_fizz_bar
method, call them in order and run bar conditionally upon success of running fizz.EDIT:
It seems like different unit test frameworks behave differently. For JUnit in Eclipse, it seems that the test cases run in random order: Ordering unit tests in Eclipse's JUnit view
使用低排序字母名称命名要首先运行的测试。
对于代码可读性来说,这可能被认为是丑陋的,也可能是有用的,具体取决于您的观点。
Name the tests you want to run first with a low-sorting alphabetical name.
For code readability, this could be considered ugly, or helpful, depending on your point of view.
您可以使用
Test::Unit::TestCase#test_order = :define
定义测试顺序示例:
结果:
如果没有
test_order = :define
,您将得到字母顺序:You can define the test order with
Test::Unit::TestCase#test_order = :defined
Example:
The result:
Without
test_order = :defined
you get the alphabetic order: