[PYTHON] When you want to print to standard output with print while testing with pytest

From the conclusion, adding the -s option is OK

When testing with pytest, there are times when you want to check not only the result but also the value of a variable in the middle. In conclusion, it will be output if you add the -s option.

$ pytest -s

Example 1 without -s

Consider the following code.

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

When tested without the -s option, there is no output inside the loop.

$ 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 =======================================================

Example 2 When using -s

Try adding -s with the same code. The output inside the loop is output.

$ 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 =======================================================

Recommended Posts

When you want to print to standard output with print while testing with pytest
Settings when you want to run python-mecab with travis
Test standard output with Pytest
When you want to filter with Django REST framework
Solution when you want to use cv_bridge with python3 (virtualenv)
[AWS] What to do when you want to pip with Lambda
Use aggdraw when you want to draw beautifully with pillow
When you want to register Django's initial data with relationships
Convert to a string while outputting standard output with Python subprocess
When you want to send an object with requests using flask
When you want to adjust the axis scale interval with APLpy
When you want to use it as it is when using it with lambda memo
Gist repository to use when you want to try a little with ansible
When you want to replace a column with a missing value (NaN) column by column
Wrapper when you want to output utf-8 + ansi color on Windows console
Memorandum of means when you want to make machine learning with 50 images
I want to mock datetime.datetime.now () even with pytest!
When you want to update the chrome driver.
What are you using when testing with Python?
Personal best practice template to use when you want to make MVP with Flask
[OpenCV] When you want to check if it is read properly with imread
Links to do what you want with Sublime Text
When you want to play a game via Proxy
When the variable you want to superscript with matplotlib is two or more characters
Things to do when you start developing with Django
When you want to plt.save in a for statement
Useful operation when you want to solve all problems in multiple programming languages with Codewars
If you want to use field names with hyphens when updating firestore data in python
[Django] A memorandum when you want to communicate asynchronously [Python3]
Knowledge you need to know when programming competitive programming with Python2
When you want to use python2.x on modern Gentoo Linux
When you want to hit a UNIX command on Python
How to output additional information when logging with python's logging module
Output to syslog with Loguru
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python
Decorator to silence standard output
If you want to become a data scientist, start with Kaggle
Don't write Python if you want to speed it up with Python
When it is troublesome to copy what you built with vue
When you want to keep the Sphinx documentation theme as usual
I want to output the beginning of the next month with Python
[Python] When you want to use all variables in another file
When you want to save the result of the callback function somewhere
When you run diff in python and want both returncode and output
[TensorFlow] If you want to run TensorBoard, install it with pip
When you want to sort a multidimensional list by multiple lines
Nice to meet you with python
Output to csv file with Python
Write standard output to a file
Output cell to file with Colaboratory
Key operations you want to know
I want to do ○○ with Pandas
Try to output audio with M5STACK
I want to debug with Python
I want to get angry with my mom when my memory is tight
If you want to include awsebcli with CircleCI, specify the python version
18 beautiful Python terms you want to read aloud. R18 with example sentences
Python Note: When you want to know the attributes of an object
Tips (input / output) that you should know when programming competitive programming with Python2
When you want to change the HTTP headers of Flask's test client
If you want to get multiple statistics with groupby in pandas v1