我想在pytest测试中使用stdin

时间:2016-08-02 14:23:53

标签: python pytest

PyTest文档指出stdin被重定向到null,因为没有人会想要在批处理测试上下文中进行交互式测试。这是事实,但交互并不是stdin的唯一用途。我想测试使用stdin的代码,就像使用任何其他文件一样。我很高兴stdout和sterr被捕获但是如何实际上将stdin连接到io.StringIO对象以符合PyTest的方式说?

2 个答案:

答案 0 :(得分:5)

您可以mock it

def test_method(monkeypatch):
    monkeypatch.setattr('sys.stdin', io.StringIO('my input'))
    # test code

答案 1 :(得分:0)

也许您可以将脚本作为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'' 来运行?在Python 3.6中:

fs.rename("${nombreHtml}.html",(err)=>{
                console.log(err)
            })
fs.appendFileSync("${nombreHtml}.html", htmlSeparado, () => { })
相关问题