# Capturing the stdout

**URL:** <https://www.ruby-forum.com/t/capturing-the-stdout/55351>\
**Category:** Ruby\
**Created:** [March 20, 2006, 6:31am UTC](https://www.ruby-forum.com/t/capturing-the-stdout/55351 "2006-03-20T06:31:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navya\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8baadc/32.png) [@Navya\_A](https://www.ruby-forum.com/u/Navya_A)\
**Post date:** [March 20, 2006, 6:31am UTC](https://www.ruby-forum.com/t/capturing-the-stdout/55351/1 "2006-03-20T06:31:11Z")

</div>

Hi all,

I am running a test case and need it capture the output it gives me.

require ‘stringio’  
old\_stdout = $stdout  
$stdout = StringIO.new  
…  
@reqout = $stdout.string  
$stdout = old\_stdout

the above code captures the stdout if something written explicily (like  
puts).  
but it doesnot capture the output or running a testcase.  
i want to capture the results if the testcases.  
is there a way it could be done.

Thanks,  
Navya.

---

<div class="post-metadata">

**Author:** ![Navya\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8baadc/32.png) [@Navya\_A](https://www.ruby-forum.com/u/Navya_A)\
**Post date:** [March 20, 2006, 11:39am UTC](https://www.ruby-forum.com/t/capturing-the-stdout/55351/2 "2006-03-20T11:39:06Z")

</div>

Navya A. wrote:

> the above code captures the stdout if something written explicily (like puts).  
> but it doesnot capture the output or running a testcase.  
> i want to capture the results if the testcases.  
> is there a way it could be done.

You probably must redirect $defout also. Other than that doing it  
externally (i.e. in the calling process) might be an option, too.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Navya\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8baadc/32.png) [@Navya\_A](https://www.ruby-forum.com/u/Navya_A)\
**Post date:** [March 20, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/capturing-the-stdout/55351/3 "2006-03-20T13:12:37Z")

</div>

On Mon, Mar 20, 2006 at 07:39:02PM +0900, Robert K. wrote:

> > the above code captures the stdout if something written explicily (like  
> > puts).  
> > but it doesnot capture the output or running a testcase.  
> > i want to capture the results if the testcases.  
> > is there a way it could be done.
> 
> You probably must redirect $defout also. Other than that doing it  
> externally (i.e. in the calling process) might be an option, too.

IIRC $defout is deprecated.

You can separate the output (from the code being tested) from the  
information  
displayed by Test::Unit as follows:

include Test::Unit::UI  
out = StringIO.new “”  
test\_out = StringIO.new “”

$stdout = out  
ret = Console::TestRunner.new(my\_test\_suite, NORMAL, test\_out).start  
$stdout = STDOUT

# “actual” stdout in out.string

# Test::Unit info in test\_out.string

If you also want to capture the stdout from subprocesses, you need  
something  
like  
begin  
old\_stdout = STDOUT.dup  
STDOUT.reopen(“mytempfile”, “w”)  
…  
# create the TestRunner, possibly redirecting its output as shown  
above;  
# fork, system, ``, etc. in the tested code will be redirected to  
the  
# tempfile  
…  
ensure  
STDOUT.reopen(old\_stdout)  
end

Silly example of the former:

RUBY\_VERSION # =\> “1.8.4”  
require ‘stringio’  
require ‘test/unit/ui/console/testrunner’

class Foo  
def foo(a,b)  
if b \> 3  
puts a + b  
else  
puts a - b  
end

```
a * b

```

end  
end

class TC\_Foo \< Test::Unit::TestCase  
def setup; @foo = Foo.new end

def test\_foo  
assert\_equal(8, @foo.foo(2, 4))  
end  
end

include Test::Unit::UI  
out = StringIO.new “”  
test\_out = StringIO.new “”

$stdout = out  
ret = Console::TestRunner.new(TC\_Foo.suite, NORMAL, test\_out).start  
$stdout = STDOUT

puts “Output:”  
out.string.each{|x| puts “-\> #{x}”}  
puts “----”  
puts “Test (#{ret.passed? ? “passed” : “failed”}):”  
test\_out.string.each{|x| puts “-\>\> #{x}”}

# \>\> Output:

# \>\> -\> 6

# \>\> ----

# \>\> Test (passed):

# \>\> -\>\> Loaded suite TC\_Foo

# \>\> -\>\> Started

# \>\> -\>\> .

# \>\> -\>\> Finished in 0.000601 seconds.

# \>\> -\>\>

# \>\> -\>\> 1 tests, 1 assertions, 0 failures, 0 errors
