python cli_测试Python命令行(CLI)应用程序的4种技术

python cli

You’ve just finished building your first Python command-line app. Or maybe your second or third. You’ve been learning Python for a while, and now you’re ready to build something bigger and more complex, but still runnable on a command-line. Or you are used to building and testing web applications or desktop apps with a GUI, but now are starting to build CLI applications.

您刚刚完成了第一个Python命令行应用程序的构建。 或者您的第二或第三名。 您已经学习Python已有一段时间了,现在您可以构建更大,更复杂的东西,但是仍然可以在命令行上运行。 或者,您习惯于使用GUI构建和测试Web应用程序或桌面应用程序,但是现在开始构建CLI应用程序。

In all these situations and more, you will need to learn and get comfortable with the various methods for testing a Python CLI application.

在所有这些情况下以及更多情况下,您将需要学习并熟悉测试Python CLI应用程序的各种方法。

While the tooling choices can be intimidating, the main thing to keep in mind is that you’re just comparing the outputs your code generates to the outputs you expect. Everything follows from that.

尽管工具的选择可能令人生畏,但要记住的主要事情是,您只是将代码生成的输出与期望的输出进行比较。 一切都从此开始。

In this tutorial you’ll learn four hands-on techniques for testing Python command-line apps:

在本教程中,您将学习四种测试Python命令行应用程序的动手技术:

  • “Lo-Fi” debugging with print()
  • Using a visual Python debugger
  • Unit testing with pytest and mocks
  • Integration testing
  • 使用print() “ Lo-Fi”调试
  • 使用可视化Python调试器
  • 使用pytest和模拟进行单元测试
  • 整合测试

Free Bonus: Click here to get our Python Testing Cheat Sheet that summarizes the techniques demonstrated in this tutorial.

免费奖金: 单击此处以获取我们的Python测试速查表 ,该总结了本教程中演示的技术。

Everything will be structured around a basic Python CLI app that passes data in the form of a multi-level dictionary to two functions that transform it in some way, then prints it to the user.

一切将围绕一个基本的Python CLI应用程序进行结构化,该应用程序将数据以多级字典的形式传递给两个函数,这些函数以某种方式对其进行转换,然后将其打印给用户。

We will use the code below to examine a few of the different methods that will aid you in testing. And while certainly not exhaustive, I hope this tutorial will give you enough breadth to get you confident in creating effective tests in the major testing domains.

我们将使用下面的代码来检查一些有助于您进行测试的不同方法。 尽管当然不能穷举,但我希望本教程能给您足够的广度,以使您有信心在主要测试领域中创建有效的测试。

I’ve sprinkled in a few bugs in this initial code, which we will expose with our testing methods.

我在初始代码中散布了一些错误,我们将通过测试方法来揭示这些错误。

Note: For simplicity’s sake, this code does not include some basic best practices, such as verifying the existence of keys in a dictionary.

注意 :为了简单起见,此代码不包括一些基本的最佳实践,例如,验证字典中键的存在。

As a first step, let’s think about our objects at every stage of this application. We start with a structure that describes John Q. Public:

首先,让我们考虑一下该应用程序每个阶段的对象。 我们从描述John Q. Public的结构开始。

 JOHN_DATA JOHN_DATA = = {
     
    {
     
    'name''name' : : 'John Q. Public''John Q. Public' ,
    ,
    'street''street' : : '123 Main St.''123 Main St.' ,
    ,
    'city''city' : : 'Anytown''Anytown' ,
    ,
    'state''state' : : 'FL''FL' ,
    ,
    'zip''zip' : : 9999999999 ,
    ,
    'relationships''relationships' : : {
     
        {
     
        'siblings''siblings' : : [[ 'Michael R. Public''Michael R. Public' , , 'Suzy Q. Public''Suzy Q. Public' ],
        ],
        'parents''parents' : : [[ 'John Q. Public Sr.''John Q. Public Sr.' , , 'Mary S. Public''Mary S. Public' ],
    ],
    }
}
}
}

We then flatten the other dictionaries, expecting this after calling our first transform function, initial_transform:

然后,我们将其他字典拼合,在调用我们的第一个转换函数initial_transform后期望这样做:

Then we build all the address information into a single address entry with the function final_transform:

然后,我们使用final_transform函数将所有地址信息构建到单个地址条目中:

 JOHN_DATA JOHN_DATA = = {
     
    {
     
    'name''name' : : 'John Q. Public''John Q. Public' ,
    ,
    'address''address' : : '123 Main St. '123 Main St.  nn Anytown, FL 99999'
    Anytown, FL 99999'
    'siblings''siblings' : : [[ 'Michael R. Public''Michael R. Public' , , 'Suzy Q. Public''Suzy Q. Public' ],
    ],
    'parents''parents' : : [[ 'John Q. Public Sr.''John Q. Public Sr.' , , 'Mary S. Public''Mary S. Public' ],
],
}
}

And the call to print_person will write this to the console:

调用print_person会将其写入控制台:

testapp.py:

testapp.py:

 def def initial_transforminitial_transform (( datadata ):
    ):
    """
"""
    Flatten nested dicts
    Flatten nested dicts
    """
        """
    for for item item in in listlist (( datadata ):
        ):
        if if typetype (( itemitem ) ) is is dictdict :
            :
            for for key key in in itemitem :
                :
                datadata [[ keykey ] ] = = itemitem [[ keykey ]

    ]

    return return data


data


def def final_transformfinal_transform (( transformed_datatransformed_data ):
    ):
    """
"""
    Transform address structures into a single structure
    Transform address structures into a single structure
    """
        """
    transformed_datatransformed_data [[ 'address''address' ] ] = = strstr .. formatformat (
        (
        "{0}"{0} nn {1}, {2} {3}"{1}, {2} {3}" , , transformed_datatransformed_data [[ 'street''street' ], 
        ], 
        transformed_datatransformed_data [[ 'state''state' ], ], transformed_datatransformed_data [[ 'city''city' ], 
        ], 
        transformed_datatransformed_data [[ 'zip''zip' ])

    ])

    return return transformed_data


transformed_data


def def print_personprint_person (( person_dataperson_data ):
    ):
    parents parents = = "and""and" .. joinjoin (( person_dataperson_data [[ 'parents''parents' ])
    ])
    siblings siblings = = "and""and" .. joinjoin (( person_dataperson_data [[ 'siblings''siblings' ])
    ])
    person_string person_string = = strstr .. formatformat (
        (
        "Hello, my name is {0}, my siblings are {1}, "
        "Hello, my name is {0}, my siblings are {1}, "
        "my parents are {2}, and my mailing"
        "my parents are {2}, and my mailing"
        "address is: "address is:  nn {3}"{3}" , 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值