[lang-ref] ( mock_standard_input_from_pipe ) ( python )

def test_mock_standard_input_from_pipe(monkeypatch):
    # monkeypatch: sys.stdin
    # This is pytest's 'monkeypatch' fixture
    # Assumed usage: `cat a.txt | ./a.py`
    import sys, io

    text = 'A1,B1,C1\nA2,B2,C2\nA3,B3,C3\n'

    monkeypatch.setattr(sys, 'stdin', io.StringIO(text))

    rows = []
    for line in sys.stdin:
        row = line.rstrip('\n').split(',')
        rows.append(row)

    assert rows == [['A1','B1','C1'],['A2','B2','C2'],['A3','B3','C3']]