我有一个文件my_test.rb
,内容如下:
require 'test/unit'
class MyTest < Test::Unit::TestCase
# Called before every test method runs. Can be used
# to set up fixture information.
def setup
# Do nothing
end
# Called after every test method runs. Can be used to tear
# down fixture information.
def teardown
# Do nothing
end
# Fake test
def dummy_test
print "Something!"
fail
end
end
当我运行ruby my_test.rb
时,绝对没有输出。
如何在此文件中运行单元测试,看看它们是通过还是失败?
答案 0 :(得分:1)
您的实现和实际代码没有什么不好的。但是,为了使您的dummy_test
执行其定义中的内容,应首先调用它;您没有做的事情,这就是为什么在运行文件时没有任何输出。
您可以创建一个示例测试,然后调用您的dummy_test方法:
...
def test_something
dummy_test
end
# Fake test
def dummy_test
print "Something!"
fail
end
答案 1 :(得分:1)
测试单元将查找以“ test”开始 的方法,并将其用作测试方法。您的代码的方法以结尾以“ test”结尾,但是没有一个以它开头的方法,因此它看不到要运行的任何测试。
将方法名称从.SD
更改为ifelse
,您应该会看到期望的输出。
答案 2 :(得分:0)
如前所述,您不调用函数,因此不会发生任何事情,因为ruby不会自动执行第一个/最后一个函数。
一个例子就是实现这段代码
...
def main()
print "Something!"
end
main()
如果您要调用一个调用其他函数的函数,您将执行相同的操作
示例:
def main()
other_function1()
other_function2()
end
main()
other_functions将是您在主函数中定义并调用的其他函数。