Skip to content

Instantly share code, notes, and snippets.

@diogobaeder
Created September 17, 2012 15:56
Show Gist options
  • Save diogobaeder/3738165 to your computer and use it in GitHub Desktop.
Save diogobaeder/3738165 to your computer and use it in GitHub Desktop.
Alternative to discard stdout
from contextlib import contextmanager
from StringIO import StringIO
import os
import sys
from unittest import TestCase
class MyTest(TestCase):
@contextmanager
def discard_print(self):
old_out = sys.stdout
sys.stdout = StringIO()
yield
sys.stdout = old_out
def test_my_stuff(self):
with self.discard_print():
# test my stuff here
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment