测试命令行实用程序

int*_*ted 11 python language-agnostic testing bash command-line

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

我想找一个会有类似语句的测试框架

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'
Run Code Online (Sandbox Code Playgroud)

据推测,基本测试用例将设置一个临时目录,在该目录中运行这些命令,并在拆卸时将其删除.

我更喜欢在Python中使用某些东西,因为我比其他似乎合理的候选语言更熟悉它.

我想可能会有一些使用DSL的东西会使它有效地与语言无关(或者它自己的语言,取决于你如何看待它); 但是这可能不太理想,因为我的测试技术通常涉及编写生成测试的代码.

这对谷歌来说有点困难,因为有很多关于运行测试的实用程序的信息,这与我正在寻找的东西相反.

对输出中嵌入的doctests的支持command --help将是额外的奖励:)

Ian*_*ing 13

查看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
Run Code Online (Sandbox Code Playgroud)

它也是合理的doctest可用的.