This question is Ruby related.
这个问题与Ruby有关。
Suppose I want to have the test unit for my class in the same file as it's definition. Is it possible to do so? For example, if I'd pass a "--test" argument when I run the file, I'd want it to run the test unit. Otherwise, execute normally.
假设我想将我的类的测试单元放在与其定义相同的文件中。有可能这样做吗?例如,如果我在运行文件时传递“--test”参数,我希望它运行测试单元。否则,正常执行。
Imagine a file like this:
想象一下这样的文件:
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
What code should I have in the #run unit test
section?
#run单元测试部分应该包含哪些代码?
1 个解决方案
#1
3
This can be achieved with a module:
这可以通过一个模块来实现:
#! /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
or without module, actually:
或者没有模块,实际上:
#! /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
#1
3
This can be achieved with a module:
这可以通过一个模块来实现:
#! /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
or without module, actually:
或者没有模块,实际上:
#! /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