pytest patch capture inputs code example

Example: pytest input()

import pytest

def funcion_coninputs():
    in1 = input('introduce1')
    print(in1)
    in2 = input('introduce2')
    print(in2)
    in3 = input('introduce3')
    print(in3)
    return in1, in2, in3


def geninputs():
    inputs = ['aaa', 'bbb', 'ccc']
    for in in inputs:
        yield in_

GEN = gen_inputs()

def test_funcion_con_inputs_withmonkey(monkeypatch):
    monkeypatch.setattr('builtins.input', lambda : next(GEN))
    i1, i2, i3 = funcion_con_inputs()
    assert i1 == 'aaa'
    assert i2 == 'bbb'
    assert i3 == 'ccc'