【问题标题】:Ruby: Why are no dots showing when I run tests with Test::Unit?Ruby:为什么当我使用 Test::Unit 运行测试时没有显示点?
【发布时间】:2014-09-29 16:44:43
【问题描述】:

我在玩 Ruby 的 Test::Unit::TestCase,虽然我的测试会运行、通过、失败等。我没有看到每个测试用例输出的点。这是我需要设置的配置,还是我需要指定的详细程度?

我正在运行 Ruby 2.1.0p0

作为参考,这是我正在使用的代码。它来自 Destroy All Software 截屏视频,其中练习是从头开始构建 rspec(当然不是全部):

测试:

#test_spec.rb

require 'test/unit'
require_relative 'spec'


class TestDescribe < Test::Unit::TestCase
  def test_that_it_can_pass
    describe 'some thing' do
      it 'has some property' do
      end
    end
  end

  def test_that_it_can_fail
    assert_raise(IndexError) do
      describe 'some failing thing' do 
        it 'fails' do
          raise IndexError
        end
      end
    end
  end
end


class TestAssertion < Test::Unit::TestCase
  def test_that_it_can_pass
    2.should == 2
  end

  def test_that_it_can_fail
    assert_raise(AssertionError) do
      1.should == 2
    end
  end
end

还有代码:

#spec.rb

def describe(description, &block)
  ExampleGroup.new(block).evaluate!
end


class ExampleGroup
  def initialize(block)
    @block = block
  end

  def evaluate!
    instance_eval(&@block)
  end

  def it(description, &block)
    block.call
  end
end


class Object
  def should
    DelayedAssertion.new(self)
  end
end

class DelayedAssertion
  def initialize(subject)
    @subject = subject
  end

  def ==(other)
    raise AssertionError unless @subject == other
  end
end


class AssertionError < Exception
end

使用ruby test_spec.rb运行时的输出

Run options:

# Running tests:

Finished tests in 0.004410s, 907.0295 tests/s, 453.5147 assertions/s.
4 tests, 2 assertions, 0 failures, 0 errors, 0 skips

ruby -v: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]

【问题讨论】:

  • 默认情况下,它应该显示点表示通过,E 表示错误,F 表示失败。你能发布你的输出,也许你的测试之一,包括你的 test_helper?

标签: ruby unit-testing


【解决方案1】:

我认为TestCase 代表一个测试。它不负责运行或提供 UI。我认为您正在寻找“跑步者”。在 minitest 中,您可以简单地使用 require 'minitest/autorun',而在 Test::Unit(已弃用)中,您可能会以某种方式使用 Test::Unit::Runner。我使用rspec,所以我不知道确切的细节,但 Test::Unit 和 minitest 都包含在 ruby​​ stdlib 中,因此文档应该很容易找到。

【讨论】:

    【解决方案2】:

    我升级了“测试单元”,并找回了点。在撰写本文时,版本为 3.1.8。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多