代码之家  ›  专栏  ›  技术社区  ›  Geo

如何在同一个源文件中拥有一个测试单元?

  •  1
  • Geo  · 技术社区  · 14 年前

    这个问题与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 部门?

    1 回复  |  直到 13 年前
        1
  •  3
  •   philant    14 年前

    #! /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