Python CLI程序单元测试 [英] Python CLI program unit testing

查看:133
本文介绍了Python CLI程序单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个python Command-Line-Interface程序,在进行测试时发现它很无聊,例如,以下是该程序的帮助信息:

I am working on a python Command-Line-Interface program, and I find it boring when doing testings, for example, here is the help information of the program:

usage: pyconv [-h] [-f ENCODING] [-t ENCODING] [-o file_path] file_path

Convert text file from one encoding to another.

positional arguments:
  file_path

optional arguments:
  -h, --help            show this help message and exit
  -f ENCODING, --from ENCODING
                        Encoding of source file
  -t ENCODING, --to ENCODING
                        Encoding you want
  -o file_path, --output file_path
                        Output file path

当我在程序上进行更改并想要测试时,必须打开一个终端,
键入命令(带有选项和参数),键入enter,然后查看运行时
是否发生任何错误。如果确实发生错误,我必须回到编辑器并从头到尾检查代码
,猜测错误的位置,进行小的更改,并写 print 行,
返回终端,再次运行命令...

When I made changes on the program and want to test something, I must open a terminal, type the command(with options and arguments), type enter, and see if any error occurs while running. If error really occurs, I must go back to the editor and check the code from top to end, guessing where the bug positions, make small changes, write print lines, return to the terminal, run command again...

递归。

所以我的问题是,使用CLI程序进行测试的最佳方法是什么,可以像使用普通python脚本进行单元测试一样容易地进行
吗?

So my question is, what is the best way to do testing with CLI program, can it be as easy as unit testing with normal python scripts?

推荐答案

我认为在整个程序级别上进行功能测试是完全可以的。每次测试仍然可以测试一个方面/选项。这样,您可以确定该程序确实可以整体运行。编写单元测试通常意味着您可以更快地执行测试,并且故障通常更易于解释/理解。但是单元测试通常与程序结构紧密相关,在内部更改内容时需要更多的重构工作。

I think it's perfectly fine to test functionally on a whole-program level. It's still possible to test one aspect/option per test. This way you can be sure that the program really works as a whole. Writing unit-tests usually means that you get to execute your tests quicker and that failures are usually easier to interpret/understand. But unit-tests are typically more tied to the program structure, requiring more refactoring effort when you internally change things.

无论如何,使用 py.test ,这是一个测试pyconv从latin1到utf8转换的小例子::

Anyway, using py.test, here is a little example for testing a latin1 to utf8 conversion for pyconv::

# content of test_pyconv.py

import pytest

# we reuse a bit of pytest's own testing machinery, this should eventually come
# from a separatedly installable pytest-cli plugin. 
pytest_plugins = ["pytester"]

@pytest.fixture
def run(testdir):
    def do_run(*args):
        args = ["pyconv"] + list(args)
        return testdir._run(*args)
    return do_run

def test_pyconv_latin1_to_utf8(tmpdir, run):
    input = tmpdir.join("example.txt")
    content = unicode("\xc3\xa4\xc3\xb6", "latin1")
    with input.open("wb") as f:
        f.write(content.encode("latin1"))
    output = tmpdir.join("example.txt.utf8")
    result = run("-flatin1", "-tutf8", input, "-o", output)
    assert result.ret == 0
    with output.open("rb") as f:
        newcontent = f.read()
    assert content.encode("utf8") == newcontent

安装后pytest( pip install pytest)可以这样运行::

After installing pytest ("pip install pytest") you can run it like this::

$ py.test test_pyconv.py
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.4.5dev1
collected 1 items

test_pyconv.py .

========================= 1 passed in 0.40 seconds =========================

该示例重用了pytest的一些内部机制利用pytest的固定机制进行自己的测试,请参见 http://pytest.org/latest/fixture.html。如果暂时忘记细节,则可以通过提供运行和 tmpdir来帮助您准备和运行测试这一事实来工作。如果您想玩游戏,可以尝试插入失败的断言语句或简单地断言0,然后查看回溯或发出 py.test --pdb以输入python提示符。

The example reuses some internal machinery of pytest's own testing by leveraging pytest's fixture mechanism, see http://pytest.org/latest/fixture.html. If you forget about the details for a moment, you can just work from the fact that "run" and "tmpdir" are provided for helping you to prepare and run tests. If you want to play, you can try to insert a failing assert-statement or simply "assert 0" and then look at the traceback or issue "py.test --pdb" to enter a python prompt.

这篇关于Python CLI程序单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆