我正在寻找一种在用 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'
大概基础测试用例会设置一个临时目录来运行这些命令,并在拆卸时将其删除。
我更愿意使用 Python 中的某些东西,因为我对它比对其他可能的候选语言更熟悉。
我想可能会有一些使用 DSL 的东西可以使它有效地与语言无关(或者它自己的语言,取决于你如何看待它);然而,这可能不太理想,因为我的测试技术通常涉及编写生成测试的代码。
用谷歌搜索有点困难,因为有很多关于运行测试的实用程序的信息,这与我正在寻找的有点相反。
支持嵌入在 command --help
输出中的 doctests 将是一个额外的好处:)
最佳答案
查看 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。
关于python - 测试命令行实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3088556/