Testing and STDOUT

I have a question with using Test::Unit::TestCase / RSpec (probably
applies to both I think). I would like to test the output to STDOUT from
the code that I'm testing as well as the results from it. Is there a
prefered way to go about this? Somehow directing STDOUT somewhere...?

···

--
Posted via http://www.ruby-forum.com/.

setting $stdio to a StringIO seems to be pretty productive.
-mat

···

On Jun 16, 2006, at 10:09 AM, Mike Nelson wrote:

I have a question with using Test::Unit::TestCase / RSpec (probably
applies to both I think). I would like to test the output to STDOUT from
the code that I'm testing as well as the results from it. Is there a
prefered way to go about this? Somehow directing STDOUT somewhere...?

Mat Schaffer wrote:

setting $stdio to a StringIO seems to be pretty productive.

I assume you mean $stdout.

This is what I have thus far with $stdout. It seems to be capturing the
output, but I'm having trouble getting back the output from StringIO
later. Not sure what I'm missing. This is what I have.

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'dirs'
require 'stringio'

context "In an empty directory" do
  setup do
    @testing_dir = File.join(File.dirname(__FILE__), '..', '_test_temp')
    @test_dir = File.join(@testing_dir, 'test_dir')
    Dir.mkdir @testing_dir
  end

  teardown do
    Dir.rmdir @testing_dir
  end

  specify "we should be able to create any dir" do
    puts "\n========================test first==================\n"
    stdout_orig = $stdout
    $stdout = StringIO.new
    puts "\n========================test first end==================\n"

    Dirs.create_dirs @test_dir
    Dir[@test_dir].should_not_be_empty
    Dir.rmdir @test_dir

    puts "\n========================test ==================\n"
    #$stdout.to_a.should_not_be_empty
    stdout_orig.puts "testy", $stdout.readlines, "testy end"
    $stdout.readlines.grep(/Creating/).should_not_be_empty
    $stdout = stdout_orig
    puts "\n========================test end ==================\n"
  end

end

gives,

In an empty directory

========================test first==================
testy
testy end
- we should be able to create any dir (FAILED - 1)

1)
ExpectationNotMetError in 'In an empty directory we should be able to
create any dir'
[] should not be empty
/home/mike/projects/ruby/rspace/test/dirs_spec.rb:29:in `we should be
able to create any dir'

Finished in 0.001164 seconds

1 context, 1 specification, 1 failure

···

--
Posted via http://www.ruby-forum.com/.

Its hidden:

$ ri StringIO#string
-------------------------------------------------------- StringIO#string
       strio.string -> string

···

On Jun 16, 2006, at 10:05 AM, Mike Nelson wrote:

Mat Schaffer wrote:

setting $stdio to a StringIO seems to be pretty productive.

I assume you mean $stdout.

This is what I have thus far with $stdout. It seems to be capturing the
output, but I'm having trouble getting back the output from StringIO
later. Not sure what I'm missing. This is what I have.

------------------------------------------------------------------------
      Returns underlying String object, the subject of IO.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

Eric Hodel wrote:

Its hidden:

$ ri StringIO#string

Thanks, that's the ticket!

For posterity here's what I got now. (I moved the $stdout stuff to the
setup and teardown sections so I can use it for the rest of my
specifies.)

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'dirs'
require 'stringio'

context "In an empty directory" do
  setup do
    @testing_dir = File.join(File.dirname(__FILE__), '..', '_test_temp')
    @test_dir = File.join(@testing_dir, 'test_dir')
    Dir.mkdir @testing_dir

    @stdout_orig = $stdout
    $stdout = StringIO.new
  end

  teardown do
    Dir.rmdir @testing_dir

    #@stdout_orig.puts "testy", $stdout.string, "testy end"
    $stdout = @stdout_orig
  end

  specify "we should be able to create any dir" do
    Dirs.create_dirs @test_dir
    Dir[@test_dir].should_not_be_empty
    Dir.rmdir @test_dir

    $stdout.string.grep(/Creating #{@test_dir}/).should_not_be_empty
  end

end

looks pretty nice now, I love it. This gives,

$ spec -f s test/dirs_spec.rb
In an empty directory
- we should be able to create any dir

Finished in 0.000861 seconds

1 context, 1 specification, 0 failures

···

--
Posted via http://www.ruby-forum.com/.