Is there a single test for Windows batch files?

I just need something very simple, for example: "Run this command and succeed if there is" this line "somewhere in the console output, otherwise." Is there such a tool?

+7
source share
3 answers

Not that I know, but you can easily write one in another batch script.

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

sets% errorlevel% to zero if a string is found, and nonzero if a string is missing. You can then verify this with IF ERRORLEVEL 1 goto :fail and execute any code that you want after the :fail label.

If you need a compact estimate of several such lines, you can use || 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 a list of lines 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 
+4
source

I created a library for testing a Windows batch block. It is currently in its infancy, but it works, and I use it.

It is called cmdUnit and can be downloaded from the project website to a bitbucket:

https://bitbucket.org/percipio/cmdunit

+2
source

I use the following filter commands:

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

foo.in.txt :
Hi

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

+1
source

All Articles