测试getpass模块:'密码输入可能会回显'

时间:2017-09-22 09:24:21

标签: python testing pytest getpass

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

我正在使用getpass而我的应用程序没有任何问题。但是,我是测试世界的新手。

common.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 ================

任何意见都表示赞赏。

现在喋喋不休地希望系统能够让我发布并停止询问更多细节。我没有其他任何补充。

1 个答案:

答案 0 :(得分:0)

修正:getpass - &gt; input

username = input("Username: ")

并修补input而不是getpass

common.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")