【问题标题】:How to run a test as standalone?如何独立运行测试?
【发布时间】:2017-09-13 01:12:07
【问题描述】:

我想在同一个文件中组织多个测试。稍后将它们作为单个测试运行。

例子:

tests/TC_release1.rb - 包含大约 10 个测试的测试文件
ruby tests/TC_release1.rb --name test_TC_01 - 运行单个测试

问题:

测试文件中的所有测试都将被初始化(在我的例子中是 10 次)。这是因为http://test-unit.github.io/test-unit/en/Test/Unit/TestCase.html中的文档中描述的“调用顺序”

问题:

我怎样才能避免这种情况,并在独立模式下运行它?

测试文件
$ cat tests/TC_release1.rb
require 'rubygems'
require 'date'
require 'test/unit'

class TC_release1 < Test::Unit::TestCase
        def initialize(options)
                super
                puts DateTime.now.strftime("%Y-%m-%dT%H:%M:%S")+" Hello from the init method."
        end

        def test_TC_01()
                puts DateTime.now.strftime("%Y-%m-%dT%H:%M:%S")+" Start test 01"
                assert_match(/2017/, `date /T`)
                puts DateTime.now.strftime("%Y-%m-%dT%H:%M:%S")+" End test 01"
        end
        def test_TC_02()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_03()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_04()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_05()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_06()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_07()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_08()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_09()
                assert_match(/2017/, `date /T`)
        end
        def test_TC_10()
                assert_match(/2017/, `date /T`)
        end
end
只执行测试文件中的第一个测试用例
$ ruby tests/TC_release1.rb --name test_TC_01
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
2017-09-13T00:31:16 Hello from the init method.
Loaded suite tests/TC_release1
Started
2017-09-13T00:31:16 Start test 01
2017-09-13T00:31:16 End test 01
.

Finished in 0.048923 seconds.
--------------------------------------------------------------------------------------------------------------------------------------------------------

1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
--------------------------------------------------------------------------------------------------------------------------------------------------------

20.44 tests/s, 20.44 assertions/s

【问题讨论】:

  • “所有测试都将被初始化”是什么意思?如果您正在做一个只应该实际执行其中一个测试的重点单元测试。
  • 你不应该重写初始化器。如果您需要进行设置,请使用 before/setup 挂钩

标签: ruby testunit


【解决方案1】:

好的,将按照 phoet 的建议使用 setup 钩子。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多