测试命令行实用程序 [英] testing command line utilities

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

问题描述

我正在寻找一种在以bash或任何其他语言编写的命令行实用程序上运行测试的方法.

I'm looking for a way to run tests on command-line utilities written in bash, or any other language.

我想找到一个包含类似语句的测试框架

I'd like to find a testing framework that would have statements like

setup:
    command = 'do_awesome_thing'
    filename = 'testfile'
    args = ['--with', 'extra_win', '--file', filename]
    run_command command args

test_output_was_correct
    assert_output_was 'Creating awesome file "' + filename + '" with extra win.'

test_file_contains_extra_win
    assert_file_contains filename 'extra win'

假定基本测试用例将在其中运行这些命令的临时目录中建立,并在拆卸时将其删除.

Presumably the base test case would set up a temp directory in which to run these commands, and remove it at teardown.

我宁愿在Python中使用某些东西,因为我比其他可能的候选语言更加熟悉它.

I would prefer to use something in Python, since I'm much more familiar with it than with other plausible candidate languages.

我认为使用DSL可能会使某些语言有效地与语言无关(或取决于您的语言,取决于语言);但是,这可能并不理想,因为我的测试技术通常涉及编写可生成测试的代码.

I imagine that there could be something using a DSL that would make it effectively language-agnostic (or its own language, depending on how you look at it); however this might be less than ideal, since my testing techniques usually involve writing code that generates tests.

为此,用Google搜索有点困难,因为有很多关于运行测试的实用程序的信息,这与我正在寻找的东西相反.

It's a bit difficult to google for this, as there is a lot of information on utilities which run tests, which is sort of the converse of what I'm looking for.

对嵌入在command --help输出中的doctest的支持将是额外的收获:)

Support for doctests embedded in the output of command --help would be an extra bonus :)

推荐答案

查看 ScriptTest :

from scripttest import TestFileEnvironment

env = TestFileEnvironment('./scratch')

def test_script():
    env.reset()
    result = env.run('do_awesome_thing testfile --with extra_win --file %s' % filename)
    # or use a list like ['do_awesome_thing', 'testfile', ...]
    assert result.stdout.startswith('Creating awesome file')
    assert filename in result.files_created

它也可以在doctest中使用.

It's reasonably doctest-usable as well.

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

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