Refactor IO redirect decorators in tests for #139

Took insiparion from test.support.capture_* functions in standard lib.