An important part of a comprehensive automated test suite for an application is end-to-end tests. These are tests that interact with the application in the same way that the end user would do, i.e. from the user interface. These end-to-end tests would then record the program output and assert that it matches the output intended by the application creator.
For a console application, an end-to-end test would involve entering input at the command line and then recording the command-line output. In C#, this could be done by running the application from the Windows Command Prompt and redirecting standard input and standard output, as with a Unix terminal. In an automated test suite, this would require a script to run the console application and store the program output in a text file. This script could be called from a test framework such as NUnit, the test output file read and then assertions made against the output file contents.
To simplify this process, I have created a C# runner for console applications:
This C# console application runner was written as a Visual Studio 2012 solution, so cannot be opened in earlier versions of Visual Studio. All of the C# code for the console application runner is contained in a single file, however, so this code could be copied and pasted into earlier versions of Visual Studio:
The application runner is initialised with the details of the application to run. It is then supplied input lines to pass to the console application and the application output is then returned. Test assertions can then be made against the application output, in order to ensure that the console application is working as intended.
Here is a simple example of using the console application runner in end-to-end tests. I have created an console application called “ConsoleCalculator”. The name of the class containing the Main() method in the application is “CalculatorProgram”. The ConsoleCalculator application accepts string inputs for mathematical sum questions, then returns the answer to the question. The test below is for addition and is written with NUnit, but other C# testing frameworks could be used instead. The ConsoleApplicationRunner class accepts two constructor arguments for the type of the console application class and the name of application executable file (minus the “.exe” file extension):
