0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonのunittestモジュールでテスト対象コード上のprint文を標準出力する

Posted at

unittestを書いているときもprintデバッグしたいときありますよね。私はあります。
そんなときのプラクティス

class Case(unittest.TestCase)

    def setUp(self):
        # 標準出力退避
        self.original_stdout = sys.stdout
        # 標準出力をキャプチャするためのストリームをセットアップ
        sys.stdout = StringIO()

    def tearDown(self):
        # 標準出力を元に戻す
        output = sys.stdout.getvalue()
        sys.stdout = self.original_stdout
        # プリント
        print(output)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?