带有argparse的Pytest:如何提示用户进行确认?

Fra*_*anc 3 python testing pytest argparse python-3.x

我有一个CLI工具,并且想测试是否提示用户确认选择input()。这等同raw_input()于在Python 2中使用。

测试的(释义)代码如下所示:

import sys
import argparse


def confirm():
    notification_str = "Please respond with 'y' or 'n'"
    while True:
        choice = input("Confirm [Y/n]?").lower()
        if choice in 'yes' or not choice:
            return True
        if choice in 'no':
            return False
        print(notification_str)


def parse_args(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--destructive', action='store_true')
    return parser.parse_args()


def main():
    args = parse_args(sys.argv[1:])
    if args.destructive:
        if not confirm():
            sys.exit()
    do_stuff(args)


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

我正在使用pytest作为我的框架。我该如何做才能测试CLI中是否显示确认提示?如果我尝试比较,则会stdout收到错误:OSError: reading from stdin while output is captured

我要确保:

  1. 设置破坏性标志时显示确认
  2. 不显示时不显示

我将在另一个文件中使用以下代码:

import pytest
from module_name import main


def test_user_is_prompted_when_destructive_flag_is_set():
    sys.argv['', '-d']
    main()
    assert _  # What the hell goes here?


def test_user_is_not_prompted_when_destructive_flag_not_set():
    sys.argv['',]
    main()
    assert _  # And here too?
Run Code Online (Sandbox Code Playgroud)

Ste*_*uch 5

我建议使用该confirm()功能开始测试是更好的单元测试策略。这样就可以在本地对诸如input和这样的东西sys.stdio进行模拟。然后,一旦确定可以正常工作,就可以编写测试以验证是否以特定方式调用了该测试。您可以为此编写测试,并confirm()在这些测试中进行模拟。

这是一个confirm()使用pytest.parametrizemock处理用户输入和输出的单元测试:

码:

@pytest.mark.parametrize("from_user, response, output", [
    (['x', 'x', 'No'], False, "Please respond with 'y' or 'n'\n" * 2),
    ('y', True, ''),
    ('n', False, ''),
    (['x', 'y'], True, "Please respond with 'y' or 'n'\n"),
])
def test_get_from_user(from_user, response, output):
    from_user = list(from_user) if isinstance(from_user, list) else [from_user]

    with mock.patch.object(builtins, 'input', lambda x: from_user.pop(0)):
        with mock.patch('sys.stdout', new_callable=StringIO):
            assert response == confirm()
            assert output == sys.stdout.getvalue()
Run Code Online (Sandbox Code Playgroud)

How does this work?

pytest.mark.parametrize allows a test function to be easily called multple times with conditions. Here are 4 simple steps which will test most of the functionality in confirm:

@pytest.mark.parametrize("from_user, response, output", [
    (['x', 'x', 'No'], False, "Please respond with 'y' or 'n'\n" * 2),
    ('y', True, ''),
    ('n', False, ''),
    (['x', 'y'], True, "Please respond with 'y' or 'n'\n"),
])
Run Code Online (Sandbox Code Playgroud)

mock.patch can be used to temporarily replace a function in module (among other uses). In this case it is used to replace input and sys.stdout to allow inject user input, and capture printed strings

with mock.patch.object(builtins, 'input', lambda x: from_user.pop(0)):
    with mock.patch('sys.stdout', new_callable=StringIO):
Run Code Online (Sandbox Code Playgroud)

finally the function under test is run and the output of the function and any string printed are verified:

assert response == confirm()
assert output == sys.stdout.getvalue()
Run Code Online (Sandbox Code Playgroud)

Test Code (for the test code):

import sys
from io import StringIO
import pytest
from unittest import mock
import builtins

def confirm():
    notification_str = "Please respond with 'y' or 'n'"
    while True:
        choice = input("Confirm [Y/n]?").lower()
        if choice in 'yes' or not choice:
            return True
        if choice in 'no':
            return False
        print(notification_str)

@pytest.mark.parametrize("from_user, response, output", [
    (['x', 'x', 'No'], False, "Please respond with 'y' or 'n'\n" * 2),
    ('y', True, ''),
    ('n', False, ''),
    (['x', 'y'], True, "Please respond with 'y' or 'n'\n"),
])
def test_get_from_user(from_user, response, output):
    from_user = list(from_user) if isinstance(from_user, list) \
        else [from_user]
    with mock.patch.object(builtins, 'input', lambda x: from_user.pop(0)):
        with mock.patch('sys.stdout', new_callable=StringIO):
            assert response == confirm()
            assert output == sys.stdout.getvalue()

pytest.main('-x test.py'.split())
Run Code Online (Sandbox Code Playgroud)

Results:

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: C:\Users\stephen\Documents\src\testcode, inifile:
collected 4 items

test.py ....                                                             [100%]

========================== 4 passed in 0.15 seconds ===========================
Run Code Online (Sandbox Code Playgroud)

Test Calls to confirm():

To test that confirm is called when expected, and that the program responds as expected when called, you can use unittest.mock to mock confirm().

注意:在通常的单元测试方案中,confirm该文件位于不同的文件中,并且mock.patch可以sys.argv与本示例中的修补方式类似地使用。

测试代码,用于检查对的调用confirm()

import sys
import argparse

def confirm():
    pass

def parse_args(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--destructive', action='store_true')
    return parser.parse_args()


def main():
    args = parse_args(sys.argv[1:])
    if args.destructive:
        if not confirm():
            sys.exit()


import pytest
from unittest import mock

@pytest.mark.parametrize("argv, called, response", [
    ([], False, None),
    (['-d'], True, False),
    (['-d'], True, True),
])
def test_get_from_user(argv, called, response):
    global confirm
    original_confirm = confirm
    confirm = mock.Mock(return_value=response)
    with mock.patch('sys.argv', [''] + argv):
        if called and not response:
            with pytest.raises(SystemExit):
                main()
        else:
            main()

        assert confirm.called == called
    confirm = original_confirm

pytest.main('-x test.py'.split())
Run Code Online (Sandbox Code Playgroud)

结果:

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: C:\Users\stephen\Documents\src\testcode, inifile:
collected 3 items

test.py ...                                                              [100%]

========================== 3 passed in 3.26 seconds ===========================
enter code here
Run Code Online (Sandbox Code Playgroud)