2011-03-30 41 views
1

当你自己的测试套件执行完成后,会有一些结果。 我想保留在txt文件或HTML, thoes信息,但我不知道如何保存那些输出的消息, 如果有人知道,请与我分享,在此先感谢 下面的代码是我的实验,但它不起作用。如何通过ruby将测试结果重定向到txt文件?

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 
+0

使用大括号图标({})来修改您的代码。 – 2011-03-30 15:10:53

回答

0

尝试:

$stdout = $logfile 

代替:

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

这意味着要重定向标准输出到文件中。

+0

不行,伙计。非常感谢所有相同的 – user680200 2011-03-31 14:09:52

1

为什么不

ruby testfile.rb > text_output.txt 
+0

非常感谢。有没有其他的方式来重定向输出信息? – user680200 2011-03-31 14:07:31

1

我有一些时间来尝试解决一些测试。我设法把它的工作是这样的:

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和标准错误$不起作用(仅适用于正常的看跌期权)。

我希望它有帮助。

相关问题