2011-07-07 61 views

回答

4

不,我知道的,但你可以很容易地編寫一個在另一個批處理腳本。

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 
2

我已經創建了一個Windows批處理單元測試庫讀取字符串列表。目前它還處於起步階段,但它起作用,我使用它。

這就是所謂的cmdUnit,它可以從項目網站上到位桶下載:

https://bitbucket.org/percipio/cmdunit

1

我用下面的filter類型命令:

對於批處理文件foo.cmd,創建以下文件:

foo.in.txt
hello

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 

然後運行foo.test.cmd

相關問題