I want to use stdin in a pytest test

左心房为你撑大大i 提交于 2019-12-06 21:52:10

问题


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 only use of stdin. I want to test code that uses stdin just as it would use any other file. I am happy with stdout and sterr being captured but how to actually have stdin connected to an io.StringIO object say in a PyTest conformant way?


回答1:


You can mock it:

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


来源:https://stackoverflow.com/questions/38723140/i-want-to-use-stdin-in-a-pytest-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!