如何独立运行测试?

时间:2017-09-13 01:12:08

标签: ruby testunit

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

例:

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

1 个答案:

答案 0 :(得分:0)

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

感谢。