Test standard output with Pytest

I decided to use Python for business and wanted to test the standard output, so as a memo

Preparation

--Python 3 series

pip install pytest

Code to be tested

I would like to test the program below. Just use the say method to output to standard output.

hogehoge.py


class HogeHoge:
  def __init__(self, name):
    self.name = name
  
  def say(self):
    print("Hello ! My name is " + self.name + ".")

Test code

Use fixture, capfd in pytest

test_hogehoge.py


from hogehoge import HogeHoge

def test_say(capfd):
  instance = HogeHoge("Taro")
  instance.say()

  out, err = capfd.readouterr()
  assert out == "Hello ! My name is Taro.\n"
  assert err is ''
[Sun Nov 24 17:58:49 2019] Running: c:\python\python.exe -m pytest
============================== test session starts =============================== 
platform win32 -- Python 3.7.2, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: D:\Document\repository\study_pytest
collected 1 item

test\test_hogehoge.py .                                                     [100%] 

=============================== 1 passed in 0.08s ================================ 

Summary

Use capfd to test standard output. You can also get standard errors.

Recommended Posts

Test standard output with Pytest
Unit test flask with pytest
Unit test log output with python
Controlling test reruns with Luigi + pytest
Test Driven Development Startup with PySide & Pytest
When you want to print to standard output with print while testing with pytest
UnicodeEncodeError struggle with standard output of python3
Output test function docstring to report with pytest-html
Strengthen with code test ⑦
Output PDF with Django
Strengthen with code test ⑨
Use Mock with pytest
Strengthen with code test ③
Markdown output with Django
Strengthen with code test ⑤
Strengthen with code test ④
Primality test with python
Output PDF with WeasyPrint
Theme: Standard output [Difficulty ★★★★★]
Limited standard output problem
Strengthen with code test ②
Strengthen with code test ①
Strengthen with code test ⑧
Standard input / output summary
Strengthen with code test ⑨
Get standard output in real time with Python subprocess
Output log in JSON format with Python standard logging
Test embedded software with Google Test
Standard input with time limit
Try Python output with Haxe 3.2
Output to syslog with Loguru
Output large log with discord.py
[Django] Test standard LoginForm [TDD]
Load test Websocket with Locust
Decorator to silence standard output
Convert to a string while outputting standard output with Python subprocess
[Understanding / application of with syntax] Flexible switching of Python standard output destination
Test Driven Development with Django Part 3
Test Driven Development with Django Part 4
Matrix representation with Python standard input
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
Test Driven Development with Django Part 2
Write standard output to a file
Make standard output non-blocking in Python
Output cell to file with Colaboratory
Output the call graph with PyCallGraph
Comply with Python coding standard PEP8
[Note] Hello world output with python
Try to output audio with M5STACK
RPC completed with standard Python3 modules
Test Driven Development with Django Part 1
Output networkX graph with graphviz (PyGraphviz)
Test Driven Development with Django Part 5
Setting to debug test by entering the contents of the library with pytest