2

この質問はRubyに関連しています。

クラスのテストユニットを、その定義と同じファイルに入れたいとします。そうすることは可能ですか?たとえば、ファイルを実行するときに「--test」引数を渡す場合は、テストユニットを実行する必要があります。それ以外の場合は、通常どおり実行してください。

次のようなファイルを想像してみてください。

require "test/unit"

class MyClass

end

class MyTestUnit < Test::Unit::TestCase
# test MyClass here
end

if $0 == __FILE__
   if ARGV.include?("--test")
      # run unit test
   else
      # run normally
   end
end

#run unit testこのセクションにはどのようなコードが必要ですか?

4

1 に答える 1

4

これは、モジュールを使用して実現できます。

#! /usr/bin/env ruby

module Modulino
    def modulino_function
        return 0
    end
end

if ARGV[0] == "-test"
    require 'test/unit'

    class ModulinoTest < Test::Unit::TestCase
        include Modulino
        def test_modulino_function
            assert_equal(0, modulino_function)
        end
    end
else
    puts "running"
end

またはモジュールなし、実際には:

#! /usr/bin/env ruby

def my_function
    return 0
end

if ARGV[0] == "-test"
    require 'test/unit'

    class MyTest < Test::Unit::TestCase
        def test_my_function
            assert_equal(0, my_function)
        end
    end
else
    puts "running rc=".my_function()
end
于 2010-10-25T12:28:00.893 に答える