I want to use stdin in a pytest test

前端 未结 2 1775
不思量自难忘°
不思量自难忘° 2021-02-12 12:15

The PyTest documentation states that stdin is redirected to null as no-one will want to do interactive testing in a batch test context. This is true, but interactive is not the

2条回答
  •  再見小時候
    2021-02-12 12:38

    Maybe you could run your script as a subprocess? In Python 3.6:

    import subprocess
    
    def test_a_repl_session():
        comlist = ['./executable_script.py']
        script = b'input\nlines\n\n'
        res = subprocess.run(comlist, input=script,
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        assert res.returncode == 0
        assert res.stdout
        assert res.stderr == b''
    

提交回复
热议问题