Using Test::Unit to assert messages appeared on $stdout/$stderr 
Author Message
 Using Test::Unit to assert messages appeared on $stdout/$stderr

I'm sorta new to the whole unit testing thing, and my only prior exposure to
software testing is using DejaGnu/Expect.  I'm just starting to use
Test::Unit, and I have a case where a certain action should produce a
warning message.  There is no other side effect that can be tested to make
sure the condition that produced the warning was indeed exercised properly,
just the appearance of the message.

I don't see any obvious assertion in Test::Unit that allows checking of I/O
to $stdout/$stderr.  What's a good way to test for such a case?

Thanks,

  - jeff



Wed, 02 Feb 2005 03:42:19 GMT  
 Using Test::Unit to assert messages appeared on $stdout/$stderr

Quote:

> I'm sorta new to the whole unit testing thing, and my only prior exposure to
> software testing is using DejaGnu/Expect.  I'm just starting to use
> Test::Unit, and I have a case where a certain action should produce a
> warning message.  There is no other side effect that can be tested to make
> sure the condition that produced the warning was indeed exercised properly,
> just the appearance of the message.

> I don't see any obvious assertion in Test::Unit that allows checking of I/O
> to $stdout/$stderr.  What's a good way to test for such a case?

Get stringio.rb from www.allruby.com/rpkg/stringio.rb

require 'stringio.rb'

class TestSomething < Test::Unit::TestCase
  def set_up

  end

  def test_something


  end

  # you can test input as well

  def test_input
    $stdin.puts "user_command"
    $stdin.rewind


  end
end

Massimiliano



Wed, 02 Feb 2005 05:58:14 GMT  
 Using Test::Unit to assert messages appeared on $stdout/$stderr

Quote:

> I'm sorta new to the whole unit testing thing, and my only prior exposure to
> software testing is using DejaGnu/Expect.  I'm just starting to use
> Test::Unit, and I have a case where a certain action should produce a
> warning message.  There is no other side effect that can be tested to make
> sure the condition that produced the warning was indeed exercised properly,
> just the appearance of the message.

> I don't see any obvious assertion in Test::Unit that allows checking of I/O
> to $stdout/$stderr.  What's a good way to test for such a case?

If something is hard to test, that's a good indication that the design
needs to change.

In your case, your object is writing to stdout or stderr.  Because it is
accessing the output streams through a global variable, you cannot
intercept those calls and test that they are correct.

A unit test is meant to test a unit (a class, for example) in
isolation.  That means you have to be able to isolate the unit from its
environment, which means it should not read or write global variables.
In fact it should only make calls to objects that it has as instance
variables or are passed to it's methods as parameters.

I would therefore change my design so that the object under test is
given a stream object to which it writes messages.  In my unit tests, I
would pass in a mock stream object that asserts that it is called
correctly.  In my application and end-to-end functional tests, I would
pass in a reference to stdout or stderr so that output actually gets
written.

I have written a Test::Mock package that you might help you mock the
output stream object.  You can get it from here:

http://www.ruby-lang.org/~knu/cgi-bin/cvsweb.cgi/rough/lib/testmock/

Cheers,
        Nat.

--
Dr. Nathaniel Pryce, Technical Director, B13media Ltd.
Studio 3a, 22-24 Highbury Grove, London N5 2EA, UK
http://www.b13media.com



Thu, 03 Feb 2005 23:48:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using Test::Unit to assert messages appeared on $stdout/$stde rr

2. using test::unit for C++ unit tests

3. Test::Unit::Mock: Mock objects for testing with Test::Unit

4. Test::Unit: assert in loop ?

5. Python 1.4's print uses stderr instead stdout on Linux

6. ANSI Standard Messages Unit Tests

7. Using FxRuby and Test::Unit.

8. Unit testing - Using Java Mock Objects in Python?

9. what is the unit number in fortran that stands for stderr

10. Can I Connect a Unit to STDERR?

11. Can I connect stderr to a unit?

12. Piping stdout/stderr to RXQUEUE while keeping rc

 

 
Powered by phpBB® Forum Software