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.
 
  1. Rename Fac.exe to Fac.dll
  2. Create a C# console application and add references to nunit.framework and Fac.dll
  3. Optional: You can use object browser in Visual Studio .NET 2003 to look into Fac.dll for available information.
  4. Place [TestFixture] attribute at the main public class.
  5. Create a new void method called TestFacMethod. Place a [Test] attribute above this method.
  6. Place your assertion within this method to check the factorial function.
  7. Build this project, and run the nunit-gui program.
  8. 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?
 
  1. Basically you can write a simple C# Console application that utilizes the System.Diagnostics.Process to create a new process to run Fac.exe.
  2. You can pass the argument you want to Fac.exe
  3. 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.
Published Friday, September 16, 2005 6:59 PM by chuawenching
Filed under:

Comments

# re: Test a C# exe without the source code

Friday, September 16, 2005 7:39 PM by triplez
That's great. But here's a better way to test private methods within the assembly.

http://www.codeproject.com/csharp/TestNonPublicMembers.asp

Which can also be used for the 1st method. The 1st method is more appropriate to use than the 2nd method as the 2nd method is more of a hack and a way to debug your application, which isn't really the "Right Way(TM)" to do it.

Furthermore, with Visual Studio 2005 IDE itself, you don't need to rename your assembly to DLL, but instead just add a reference to the exe itself. Yes, that is now possible with Visual Studio 2005.

# re: Test a C# exe without the source code

Monday, September 19, 2005 9:47 AM by chuawenching
Cool. That is interesting. I will check it out and update the findings in my blog. :)

# pledge accutane

Monday, August 04, 2008 5:59 AM by istoretinoineen

[url=journals.aol.com/.../buy-isotretinoin-generic-accutan]buy accutane  

[/url]

# imitrex com

Tuesday, August 05, 2008 8:44 AM by sumatiptan

[url=www.xanga.com/sumatriptan_imitrex]imitrex and pregnancy[/url]