Test a C# exe without the source code
I has writen a simple illustration as below, in order to test a simple C# program
that accepts input and perform factorial calculation to it. I am hoping to hear feedback from you.
I will try to be specific and explain in steps by steps.
General Assumption 1: this is a C# console application called Fac.exe
General Assumption 2: only on .NET Framework 1.1 and Visual Studio .NET 2003
There are 2 possible ways which I can think of at the moment.
1)
Assumption 1: There is a public method in this application. You can check it through .NET Reflector by Lutz Roeder.
Rename Fac.exe to Fac.dll
Create a C# console application and add references to nunit.framework and Fac.dll
Optional: You can use object browser in Visual Studio .NET 2003 to look into Fac.dll for available information.
Place [TestFixture] attribute at the main public class.
Create a new void method called TestFacMethod. Place a [Test] attribute above this method.
Place your assertion within this method to check the factorial function.
Build this project, and run the nunit-gui program.
You can test it.
A simple snippet of the test elaborates above:
[Test]
public void TestThis()
{// Assuming Calc is the class for Fac.exe, and Fac(long input) is the public static method
long result = Calc.Fac(5);
Assert.AreNotEqual(0, result);
}
2)
Assumption 2: What if there is no public method, which will be difficult to use NUnit?
Basically you can write a simple C# Console application that utilizes the System.Diagnostics.Process to create a new process to run Fac.exe.
You can pass the argument you want to Fac.exe
You can redirect the standard output and error into a string (or a text file) to be displayed later at the console.
A simple snippet to elaborate as above:
Process mProcess = new Process();
mProcess.StartInfo.FileName = "Fac.exe";
mProcess.StartInfo.UseShellExecute = false;
mProcess.StartInfo.CreateNoWindow = true;
mProcess.StartInfo.RedirectStandardInput = true;
mProcess.StartInfo.RedirectStandardOutput = true;
mProcess.StartInfo.RedirectStandardError = true;
mProcess.Start();
StreamWriter input = mProcess.StandardInput;
StreamReader output = mProcess.StandardOutput;
StreamReader error = mProcess.StandardError;
input.Write("5" + System.Environment.NewLine);
input.Write("exit" + System.Environment.NewLine);
string sOutput = output.ReadToEnd();
string sError = error.ReadToEnd();
if (!mProcess.HasExited)
mProcess.Kill();
Console.WriteLine("Exited Code: " + mProcess.ExitCode);
input.Close();
output.Close();
error.Close();
mProcess.Close();
// Display at the console
Console.WriteLine("[Start] " + sOutput + " [End]" + System.Environment.NewLine); Console.WriteLine("[Start] " + sError + " [End]" + System.Environment.NewLine);Cheers.