Writing a pytest function for checking the output on console (stdout)

后端 未结 2 1449
南方客
南方客 2020-12-29 22:57

This link gives a description how to use pytest for capturing console outputs. I tried on this following simple code, but I get error

import sys
import pytest         


        
2条回答
  •  感情败类
    2020-12-29 23:23

    The problem is with your explicit call of your test function at the very end of your first code snippet block:

    test_add(sys.stdout)
    

    You should not do this; it is pytest's job to call your test functions. When it does, it will recognize the name capsys (or capfd, for that matter) and automatically provide a suitable pytest-internal object for you as a call argument. (The example given in the pytest documentation is quite complete as it is.)

    That object will provide the required readouterr() function. sys.stdout does not have that function, which is why your program fails.

提交回复
热议问题