0

我正在寻找有关为什么会引发以下错误的任何见解。我想知道问题是否与pytest有关?

否则,我在我的应用程序中使用 getpass 没有问题。但是,我是测试世界的新手。

常见的.py

def username_password():
    """Get login credentials"""
    # show current windows user
    print("\nThe current windows user is {}\n".format(getuser()))

    username = getpass("Username: ")
    password = getpass("Password: ")
    return username, password

test_common.py

from unittest.mock import patch
from common import username_password

@patch("getpass.getpass")
@patch("getpass.getuser")
def test_username_password(getuser, getpass):
    getuser.return_value = "Me"
    getpass.return_value = "xxx"
    assert username_password() == ("Me", "xxx")

命令行

py.test test_common.py --cov  --cov-report term-missing
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: C:\Users\JB\Desktop\Coding\Bot\Bot_tests, inifile:
plugins: cov-2.5.1
collected 17 items

test_common.py ....F..x.........

================================== FAILURES ===================================
___________________________ test_username_password ____________________________

getuser = <MagicMock name='getuser' id='1759491735280'>
getpass = <MagicMock name='getpass' id='1759491732424'>

    @patch("getpass.getpass")
    @patch("getpass.getuser")
    def test_username_password(getuser, getpass):
        getuser.return_value = "Me"
        getpass.return_value = "xxx"
>       u, p = username_password()

test_common.py:65:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Bot\common.py:29: in username_password
    username = getpass("Username: ")
..\..\environments\ipython_env\Anaconda3\lib\getpass.py:101: in win_getpass
    return fallback_getpass(prompt, stream)
..\..\environments\ipython_env\Anaconda3\lib\getpass.py:127: in fallback_getpass
    return _raw_input(prompt, stream)
..\..\environments\ipython_env\Anaconda3\lib\getpass.py:147: in _raw_input
    line = input.readline()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.capture.DontReadFromInput object at 0x00000199A8BD37F0>
args = ()

    def read(self, *args):
>       raise IOError("reading from stdin while output is captured")
E       OSError: reading from stdin while output is captured

..\..\environments\ipython_env\Anaconda3\lib\site-packages\_pytest\capture.py:433: OSError
---------------------------- Captured stdout call -----------------------------

The current windows user is JB

---------------------------- Captured stderr call -----------------------------
C:\Users\JB\Desktop\Coding\environments\ipython_env\Anaconda3\lib\getpass.py:101: GetPassWarning: Can not control echo on the terminal.
  return fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
Username:
=============== 1 failed, 15 passed, 1 xfailed in 0.50 seconds ================

任何输入表示赞赏。

现在喋喋不休地希望系统能够让我发布并停止询问更多细节。我没有什么要补充的了。

4

2 回答 2

1

修复:getpass-> input

username = input("Username: ")

和补丁input而不是getpass

常见的.py:

def username_password():
    """Get login credentials"""
    # show current windows user
    print("\nThe current windows user is {}\n".format(getuser()))

    username = input("Username: ")
    password = getpass("Password: ")
    return username, password

test_common.py:

from unittest.mock import patch
from common import username_password

@patch("builtins.input")
@patch("getpass.getpass")
def test_username_password(input, getpass):
    input.return_value = "Me"
    getpass.return_value = "xxx"
    assert username_password() == ("Me", "xxx")
于 2017-09-22T15:59:30.760 回答
0

为此,我必须导入 getpass 并显式使用该方法,然后使用带有可迭代的猴子补丁。

常见的.py

import getpass

def username_password():
"""Get login credentials"""
    # show current windows user
    print("\nThe current windows user is {}\n".format(getuser()))

    username = getpass.getpass("Username: ")
    password = getpass.getpass("Password: ")
    return username, password

test_common.py

from common import username_password

def test_username_password(monkeypatch):
    responses = iter(['Me', 'xxx'])
    monkeypatch.setattr('getpass.getpass', lambda _: next(responses))    
    u, p = username_password()
    assert u == "Me"
    assert p == "xxx"
于 2022-03-04T16:59:56.507 回答