Unit testing 是否有针对Windows批处理文件的单元测试框架?

Unit testing 是否有针对Windows批处理文件的单元测试框架?,unit-testing,batch-file,automated-tests,Unit Testing,Batch File,Automated Tests,我只需要一些非常简单的东西,比如“运行这个命令,如果控制台输出中有‘thistring’,那么就成功,否则就失败”。有这样的工具吗?我不知道,但您可以轻松地在另一个批处理脚本中编写一个 call TestBatchScript.cmd > console_output.txt findstr /C:"this string" console_output.txt 如果找到字符串,则将%errorlevel%设置为零;如果缺少字符串,则将设置为非零。然后,您可以使用IF ERRORLEVE

我只需要一些非常简单的东西,比如“运行这个命令,如果控制台输出中有‘thistring’,那么就成功,否则就失败”。有这样的工具吗?

我不知道,但您可以轻松地在另一个批处理脚本中编写一个

call TestBatchScript.cmd > console_output.txt
findstr /C:"this string" console_output.txt
如果找到字符串,则将%errorlevel%设置为零;如果缺少字符串,则将设置为非零。然后,您可以使用
IF ERRORLEVEL 1 goto:fail
测试这一点,并在
:fail
标签之后执行您想要的任何代码

如果要对多个这样的字符串进行紧凑计算,可以使用| |语法:

call TestBatchScript.cmd > console_output.txt
findstr /C:"teststring1" console_output.txt || goto :fail
findstr /C:"teststring2" console_output.txt || goto :fail
findstr /C:"teststring3" console_output.txt || goto :fail
findstr /C:"teststring4" console_output.txt || goto :fail
goto :eof

:fail
echo You Suck!
goto :eof
或者,您可以更进一步,从文件中读取字符串列表

call TestBatchScript.cmd > console_output.txt
set success=1
for /f "tokens=*" %%a in (teststrings.txt) do findstr /C:"%%a" console_output.txt || call :fail %%a
if %success% NEQ 1 echo You Suck!
goto :eof

:fail
echo Didn't find string "%*"
set success=0
goto :eof

我已经为windows批处理单元测试创建了一个库。它目前还处于初级阶段,但它很有效,我也在使用它

它称为cmdUnit,可以从bitbucket上的项目站点下载:

我使用以下类型命令:

对于批处理文件
foo.cmd
,创建以下文件:

foo.in.txt

你好

foo.expected.txt

你好,世界

foo.test.cmd

@echo off

echo Testing foo.cmd ^< foo.in.txt ^> foo.out.txt

call foo.cmd < foo.in.txt > foo.out.txt || exit /b 1

:: fc compares the output and the expected output files:
call fc foo.out.txt foo.expected.txt || exit /b 1

exit /b 0
@echo关闭
回声测试foo.cmd^foo.out.txt
调用foo.cmdfoo.out.txt | |退出/b 1
::fc比较输出和预期的输出文件:
调用fc foo.out.txt foo.expected.txt | |退出/b 1
退出/b0
然后运行
foo.test.cmd

另请参见