32
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pytestでテスト中にprintで標準出力したいとき

Posted at

##結論から -s オプション追加でOK

pytestでテストするとき、単純に結果だけでなく途中の変数の値などを調べたいときがあります。
結論から言うと-sオプションを追加すれば出力されます。

$ pytest -s

例1 -sを使わない場合

以下のようなコードを考えます。

test_one.py
# test_one.py
def test_good():
    for i in range(5):
        print(i)
    assert True


def test_bad():
    print('this should fail!')
    assert False

-sオプションなしでテストすると、ループの中のアウトプットは出ません。

$ pytest
=========================================================== test session starts ===========================================================
platform darwin -- Python 3.7.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /Users/reishimitani/Desktop/d2xx/D2XX
collected 2 items

test_one.py .F                                                                                                                      [100%]

================================================================ FAILURES =================================================================
________________________________________________________________ test_bad _________________________________________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

test_one.py:10: AssertionError
---------------------------------------------------------- Captured stdout call -----------------------------------------------------------
this should fail!
========================================================= short test summary info =========================================================
FAILED test_one.py::test_bad - assert False
======================================================= 1 failed, 1 passed in 0.37s =======================================================

例2 -sを使う場合

同じコードで-sを追加して試してみます。ループの中のアウトプットが出力されます。

$ pytest -s
=========================================================== test session starts ===========================================================
platform darwin -- Python 3.7.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /Users/reishimitani/Desktop/d2xx/D2XX
collected 2 items

test_one.py 0
1
2
3
4
.this should fail!
F

================================================================ FAILURES =================================================================
________________________________________________________________ test_bad _________________________________________________________________

    def test_bad():
        print('this should fail!')
>       assert False
E       assert False

test_one.py:10: AssertionError
========================================================= short test summary info =========================================================
FAILED test_one.py::test_bad - assert False
======================================================= 1 failed, 1 passed in 0.34s =======================================================
32
12
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
32
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?