Python CLI程序单元测试

编程入门 行业动态 更新时间:2024-10-11 21:24:19
本文介绍了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的固定机制进行自己的测试,请参见 pytest/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 pytest/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程序单元测试

本文发布于:2023-11-24 02:24:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   程序   Python   CLI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!