Ruby 运行minitest不会产生任何输出

Ruby 运行minitest不会产生任何输出,ruby,minitest,Ruby,Minitest,我有一个文件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

我有一个文件
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
要求“测试/单元”
类MyTest
当我运行
ruby my_test.rb
时,绝对没有输出


如何运行此文件中的单元测试并查看它们是通过还是失败?

您的实现和实际代码都没有问题。但是为了让你的
dummy\u测试能够执行它定义中的内容,应该首先调用它;你们并没有做的事情,这就是为什么当你们运行你们的文件时你们并没有得到任何输出

您可以创建一个示例测试,并在其中调用您的虚拟测试方法:

...

def test_something
  dummy_test
end

# Fake test
def dummy_test
  print "Something!"
  fail
end

如前所述,您没有调用函数,因此不会发生任何事情,因为ruby不会自动执行第一个/最后一个函数

一个例子就是实现这段代码

...
def main()
    print "Something!"
end

main()
如果你想调用一个调用其他函数的函数,你也会这么做

例如:

def main()
    other_function1()
    other_function2()
end

main()

其他函数是您在主函数中定义和调用的其他函数。

测试单元将查找以“Test”开头的方法,并将其用作测试方法。您的代码有一个以“test”结尾的方法,但没有一个以它开头的方法,因此它看不到任何要运行的测试

将方法名称从
dummy\u test
更改为
test\u dummy
,您应该会看到预期的输出。

注意和是不同的测试库。您的代码正在使用测试单元,但您的标题显示minitest。