Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a unit-test framework for Windows batch files?

I need just something very simple like "run this command and succeed if there is 'this string' somewhere in the console output, fail otherwise". Is there such a tool?

like image 697
Karel Smutný Avatar asked Jul 07 '11 14:07

Karel Smutný


2 Answers

Not that I'm aware of, but you can easily write one in another batch script.

call TestBatchScript.cmd > console_output.txt
findstr /C:"this string" console_output.txt

will set %errorlevel% to zero if the string is found, and nonzero if the string is absent. You can then test that with IF ERRORLEVEL 1 goto :fail and execute whatever code you want after the :fail label.

If you want compact evaluation of several such strings, you can use the || syntax:

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

Or, you can go even further and read the list of strings from a file

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
like image 123
Ryan Bemrose Avatar answered Oct 12 '22 23:10

Ryan Bemrose


I use the following for filter type commands:

For batch file foo.cmd, create the following files:

foo.in.txt:
hello

foo.expected.txt:
hello world

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

Then run foo.test.cmd

like image 20
Neil McGuigan Avatar answered Oct 13 '22 00:10

Neil McGuigan