是否可以对 CMD/批处理文件的结果进行单元测试或断言?
我编写了一段代码,用于解压缩压缩的 .cmd 文件并运行它们。
我想对其进行单元测试。到目前为止,对于测试,我已经创建了一个非常简单的 cmd 文件(“echo hello world”),保存并压缩它并用我的代码处理它。我可以验证它是否有效,因为如果我将“echo hello world”替换为“pause”,则会出现一个带有暂停命令的命令窗口。但这显然不是自动化的。
有什么方法可以自动化这个测试,以便它断言某些东西吗?例如,运行不同的 DOS 命令?
干杯, 马特
I've written a block of code that's supposed to unzip compressed .cmd files and run them.
I'd like to unit test it. So far for the test I've created a very simple cmd file ("echo hello world") saved it and zipped it and processed it with my code. I can verify it works because if I replace "echo hello world" with "pause" a command window appears with the pause command. But that's obviously not automated.
Is there any way I can automate this test so that it asserts something? Running a different DOS command for example?
Cheers,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将批处理文件更改为
echo Everything > %temp%\MyResult.txt
,然后检查该文件是否存在并且内容是否正确。(并删除前后文件)
Change the batch file to
echo Whatever > %temp%\MyResult.txt
, then check that the file exists and has the correct contents.(And delete the file both before and after)
基本上,您可以执行现有的“echo hello world”批处理文件并检查进程的退出代码。
根据约定,退出代码 0 表示命令(或您的情况下的批处理文件)成功执行。
如果您的批处理文件有效(即正确解压缩)、存在(即正确解压缩到预期位置),则不会强制退出代码不等于 0(例如通过
exit /b 1
或通过无效语法等),退出代码将始终为 0。在伪代码中,您的测试可能如下所示:
其中 UnzipAndExecuteBatch() 是您要测试的实际代码。可能被修改了,所以它返回cmd.exe的退出代码。
Basically, you could just execute your existing "echo hello world" batch file and check the process' exit code.
Per convention an exit code of 0 means that the command (or batch file in your case) executed successfully.
If your batch file is valid (i.e. correctly unzipped), exists (i.e. correctly unzipped to expected location), does not force an exit code not equal to 0 (e.g. via
exit /b 1
or via invalid syntax, etc.), the exit code will always be 0.In pseudo code your test could look like this:
Where UnzipAndExecuteBatch() would be your actual code, that you want to test. Probably modified, so it returns the exit code of cmd.exe.