【问题标题】:How to redirect test result to txt file by ruby?如何通过 ruby​​ 将测试结果重定向到 txt 文件?
【发布时间】:2011-03-30 15:02:00
【问题描述】:

当您自己的测试套件执行完毕后,会有一些结果。 我想将这些信息保存在 txt 文件或 html 中, 但我不知道如何保存那些输出的消息, 如果有人知道,请与我分享,提前谢谢 以下代码是我的实验,但它不起作用。

require File.join(File.dirname(__FILE__),  'person')
require "test/unit"

filename = "logfile.txt"
$logfile = File.new(filename,"a")
open(logfile,'a') { |f| f << $stdout}

class PersonTest < Test::Unit::TestCase

  FIRST_NAME, LAST_NAME, AGE = 'Nathaniel', 'Taldtretbott', 25

  def setup
    @person = Person.new(FIRST_NAME, LAST_NAME, AGE)
  end

  def test_first_name
    assert_equal "asv", @person.first_name,"try to compare"
  end

  def test_last_name
    assert_equal "Taldtretbott", @person.last_name
  end

  def test_full_name
    assert_equal FIRST_NAME + ' ' + LAST_NAME, @person.full_name
  end
end

【问题讨论】:

  • 使用大括号图标 ({}) 来查找您的代码。

标签: ruby unit-testing testing automated-tests testunit


【解决方案1】:

为什么不这样做

ruby testfile.rb > text_output.txt

【讨论】:

  • 非常感谢。还有其他方法可以重定向输出信息吗?
【解决方案2】:

我有时间尝试对解决方案进行一些测试。我设法让它以这种方式工作:

require 'test/unit'

STDOUT = $stdout = File.open("stdout.txt", "a+")
STDERR = $stderr = File.open("stderr.txt", "a+")

class AreasTest < Test::Unit::TestCase
  def test_ok
    puts "#{Time.now} Hello "
    a = 10
    assert_equal(a, 10)
  end


  def test_fail
    a = 9
    assert_equal(a, 10)
  end
end

它给出一个警告,因为 STDOUT 和 STDERR 已经初始化,但只是重定向 $stdout 和 $stderr 不起作用(仅适用于正常的 put)。

希望对你有帮助。

【讨论】:

    【解决方案3】:

    试试:

    $stdout = $logfile
    

    代替:

    open(logfile,'a') { |f| f << $stdout}
    

    这意味着您正在将标准输出重定向到文件。

    【讨论】:

      猜你喜欢
      • 2022-12-29
      • 1970-01-01
      • 2019-05-27
      • 2017-10-31
      • 1970-01-01
      • 2018-09-18
      • 2011-05-05
      • 1970-01-01
      相关资源
      最近更新 更多