Simple script for black-box integration test of console applications

suggest change

This example automates a test for a console application that uses standard input and standard output.

The application reads and sums each line, then writes the result after a blank line. The PowerShell script writes pass when the output matches. Close standard input before ReadToEnd, or the read waits until the process exits.

$process = New-Object System.Diagnostics.Process
$process.StartInfo.FileName = ".\ConsoleApp1.exe"
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardInput = $true
if ( $process.Start() ) {
    # input
    $process.StandardInput.WriteLine("1");
    $process.StandardInput.WriteLine("2");
    $process.StandardInput.WriteLine("3");
    $process.StandardInput.WriteLine();
    $process.StandardInput.WriteLine();
    $process.StandardInput.Close()
    # output check
    $output = $process.StandardOutput.ReadToEnd()
    if ( $output ) {
        if ( $output.Contains("sum 6") ) {
            Write "pass"
        }
        else {
            Write-Error $output
        }
    }
    $process.WaitForExit()
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents