Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多个输入测试单击应用程序提示_Python_Unit Testing_Command Line Interface_Pytest_Python Click - Fatal编程技术网

Python 使用多个输入测试单击应用程序提示

Python 使用多个输入测试单击应用程序提示,python,unit-testing,command-line-interface,pytest,python-click,Python,Unit Testing,Command Line Interface,Pytest,Python Click,我已经使用Click和Python编写了一个小的命令行应用程序,我现在正尝试为其编写测试(主要是为了学习如何测试Click应用程序,以便我可以继续测试我正在开发的应用程序) 下面是我要测试的一个函数: @click.group() def main(): pass @main.command() @click.option('--folder', '-f', prompt="What do you want to name the folder? (No spaces plea

我已经使用Click和Python编写了一个小的命令行应用程序,我现在正尝试为其编写测试(主要是为了学习如何测试Click应用程序,以便我可以继续测试我正在开发的应用程序)

下面是我要测试的一个函数:

@click.group()
def main():
    pass    

@main.command()
@click.option('--folder', '-f', prompt="What do you want to name the folder? (No spaces please)")
def create_folder(folder):
    while True:
        if " " in folder:
            click.echo("Please enter a name with no spaces.")
            folder = click.prompt("What do you want to name the folder?", type=str)
        if folder in os.listdir():
            click.echo("This folder already exists.")
            folder = click.prompt("Please choose a different name for the folder")
        else:
            break

    os.mkdir(folder)
    click.echo("Your folder has been created!")
我正在尝试使用Click的内置测试(以及更多详细信息)和pytest来测试这一点。这在我测试一个可接受的文件夹名(即没有空格且不存在的文件夹名)的情况下效果很好。见下文:

import clicky # the module we're testing
import os
from click.testing import CliRunner
import click
import pytest
input sys

runner = CliRunner()
folder = "myfolder"
folder_not = "my folder"

question_create = "What do you want to name the folder? (No spaces please): "
echoed = "\nYour folder has been created!\n"

def test_create_folder():
    with runner.isolated_filesystem():
        result = runner.invoke(clicky.create_folder, input=folder)
        assert folder in os.listdir()
        assert result.output == question_create + folder + echoed
我现在想测试这个函数,如果我提供了一个不允许的文件夹名,比如一个带有空格的文件夹名,然后在提示我不能有空格后,我将提供一个可接受的文件夹名。但是,我不知道如何使
click.runner
接受多个输入值,这是我唯一能想到的方法。我也愿意使用unittest mocking,但我不确定如何将其集成到Click进行测试的方式中,除了这个问题,到目前为止,这种方式工作得非常好。以下是我对多个输入的尝试:

def test_create_folder_not():
    with runner.isolated_filesystem():
        result = runner.invoke(clicky.create_folder, input=[folder_not, folder]) # here i try to provide more than one input
        assert result.output == question_create + folder_not + "\nPlease enter a name with no spaces.\n" + "What do you want to name the folder?: " + folder + echoed
我试图通过将多个输入放在一个列表中来提供多个输入,就像我在mocking中看到的那样,但我得到了以下错误:

'list' object has no attribute 'encode'

如果您对此有任何想法,我们将不胜感激

要向测试运行程序提供多个输入,只需将输入与
\n
连接起来即可,如下所示:

代码: 结果: 使用
click.ParamType
但是,我建议您可以使用
ParamType
来提示所需的文件名属性。Click提供了一个可以子类化的
ParamType
,然后传递到
Click.option()
。然后可以通过单击完成格式检查和重新提示

您可以将ParamType子类化,如:

import click

class NoSpacesFolder(click.types.StringParamType):

    def convert(self, value, param, ctx):
        folder = super(NoSpacesFolder, self).convert(value, param, ctx)
        if ' ' in folder:
            raise self.fail("No spaces allowed in selection '%s'." %
                value, param, ctx)

        if folder in os.listdir():
            raise self.fail("This folder already exists.\n"
                            "Please choose another name.", param, ctx)
        return folder
使用参数类型: 要使用自定义参数类型,请将其传递给
click.option()
类似:

@main.command()
@click.option(
    '--folder', '-f', type=NoSpacesFolder(),
    prompt="What do you want to name the folder? (No spaces please)")
def create_folder(folder):
    os.mkdir(folder)
    click.echo("Your folder has been created!")
import click

class NoSpacesFolder(click.types.StringParamType):

    def convert(self, value, param, ctx):
        folder = super(NoSpacesFolder, self).convert(value, param, ctx)
        if ' ' in folder:
            raise self.fail("No spaces allowed in selection '%s'." %
                value, param, ctx)

        if folder in os.listdir():
            raise self.fail("This folder already exists.\n"
                            "Please choose another name.", param, ctx)
        return folder
@main.command()
@click.option(
    '--folder', '-f', type=NoSpacesFolder(),
    prompt="What do you want to name the folder? (No spaces please)")
def create_folder(folder):
    os.mkdir(folder)
    click.echo("Your folder has been created!")