pytest中的getpass返回OSError:捕获输出时从stdin读取

时间:2019-04-25 20:19:38

标签: python-3.x pytest getpass

我正在尝试在pytest中编写测试以通过getpass传递密码,但出现此错误:

OSError: reading from stdin while output is captured

这是我的功能:

def test_my_password_check(monkeypatch):   
    password = 'pytestpassword'
    monkeypatch.setattr('getpass.getpass', lambda: password)
    update_passwords()
    assert keyring.get_password('testaddress', 'testusername') == password

我已经尝试过:

import mock
@mock.patch("getpass.getpass")
def test_username_password(getpass):
    password = 'pytestpassword'
    getpass.return_value = "xxx"
    update_passwords()
    assert keyring.get_password('testaddress', 'testusername') == password

即使使用from unittest.mock import patchupdate_passwords.getpass = lambda: password

所有错误相同。我想念什么?谢谢!!

0 个答案:

没有答案