My Kind of Stupid

Web development, user interfaces, and other development topics.

Silence Test::Unit tests

I've written several tests that have the unfortunate side effect of writing to stdout or stderr, polluting my pretty stream of dots.

Thanks to a post on Benevolent Code, I decided to silence them (gist).

setup :silence_output

# Redirects stderr and stdout to /dev/null.
def silence_output
  @orig_stderr = $stderr
  @orig_stdout = $stdout

  # redirect stderr and stdout to /dev/null
  $stderr = File.new('/dev/null', 'w')
  $stdout = File.new('/dev/null', 'w')
end

teardown :enable_output

# Replace stdout and stderr so anything else is output correctly.
def enable_output
  $stderr = @orig_stderr
  $stdout = @orig_stdout
  @orig_stderr = nil
  @orig_stdout = nil
end

Update:

The same thing in RSpec (gist):

RSpec.configure do |config|
  config.before(:all, &:silence_output)
  config.after(:all, &:enable_output)
end

# Redirects stderr and stdout to /dev/null.
def silence_output
  @orig_stderr = $stderr
  @orig_stdout = $stdout

  # redirect stderr and stdout to /dev/null
  $stderr = File.new('/dev/null', 'w')
  $stdout = File.new('/dev/null', 'w')
end

# Replace stdout and stderr so anything else is output correctly.
def enable_output
  $stderr = @orig_stderr
  $stdout = @orig_stdout
  @orig_stderr = nil
  @orig_stdout = nil
end