在 Ruby 中,如何控制 Test::Unit 测试的运行顺序?

发布于 2024-08-12 10:53:33 字数 507 浏览 8 评论 0原文

例如,当运行这些测试时,我想确保 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

櫻之舞 2024-08-19 10:53:34

同一测试类中的测试按照定义的顺序进行调用。但是,测试类是按类名的字母顺序运行的。

如果您确实需要精细控制,请使用 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 a test_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

枯寂 2024-08-19 10:53:34

使用低排序字母名称命名要首先运行的测试。

def test_AAA_fizz

对于代码可读性来说,这可能被认为是丑陋的,也可能是有用的,具体取决于您的观点。

Name the tests you want to run first with a low-sorting alphabetical name.

def test_AAA_fizz

For code readability, this could be considered ugly, or helpful, depending on your point of view.

可是我不能没有你 2024-08-19 10:53:33

您可以使用 Test::Unit::TestCase#test_order = :define 定义测试顺序

示例:

gem 'test-unit'  #I used 2.5.5
require 'test/unit'
class Mytest < Test::Unit::TestCase
  self.test_order = :defined
  #~ self.test_order = :random
  #~ self.test_order = :alphabetic #default
  def test_b
    p :b
  end
  def test_a
    p :a
  end
  def test_c
    p :c
  end
end

结果:

Loaded suite test
Started
:b
.:a
.:c
.

Finished in 0.001 seconds.

如果没有 test_order = :define,您将得到字母顺序:

Loaded suite test
Started
:a
.:b
.:c
.

You can define the test order with Test::Unit::TestCase#test_order = :defined

Example:

gem 'test-unit'  #I used 2.5.5
require 'test/unit'
class Mytest < Test::Unit::TestCase
  self.test_order = :defined
  #~ self.test_order = :random
  #~ self.test_order = :alphabetic #default
  def test_b
    p :b
  end
  def test_a
    p :a
  end
  def test_c
    p :c
  end
end

The result:

Loaded suite test
Started
:b
.:a
.:c
.

Finished in 0.001 seconds.

Without test_order = :defined you get the alphabetic order:

Loaded suite test
Started
:a
.:b
.:c
.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文