AymenRM
AymenRM

Reputation: 71

How can I test method without debug?

I have a method that hash input string to generate MD5 pass from it can I test his input an output without debugging

private string getMD5hash(string input)
{
    //create a new instance of MD5 object
    MD5 md5Hasher = MD5.Create(); 
    //convert the input value to byte array
    byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
    StringBuilder sBuilder = new StringBuilder();

    for (int i = 0; i < data.Length  ; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }
    return sBuilder.ToString();
}

I am using Visual Studio 2010

Upvotes: 0

Views: 170

Answers (4)

shanif
shanif

Reputation: 464

General advice is to use unit tests. That way the tests remain and you have a simple way to run them.

Upvotes: 0

Hyrum Taft
Hyrum Taft

Reputation: 21

In Visual Studio 2010/2012 Open a file in the Assembly you are working on.
Open the Immediate Window (Ctrl+D,I) or Debug -> Windows -> Immediate.


Type the full name of the method: (to bad IntelliSense doesn't work)

new ConsoleApplication1.Program().getMD5hash("stringToHash");

I have not tested this on other versions of Visual Studio. Also, remember, when the command is executed the code editor should have a file open in the project of interest. Switching to another file in another project will not allow the code to run.

Upvotes: 2

AymenRM
AymenRM

Reputation: 71

All is good

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputstr = Console.ReadLine();

           Console.WriteLine(string.Format("{0} = {1}", inputstr, getMD5hash(inputstr)));

            Console.ReadKey();



        }

        public static string getMD5hash(string input)
        {
            //create a new instance of MD5 object
            MD5 md5Hasher = MD5.Create();
            //convert the input value to byte array
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }


    }
}

Upvotes: 0

freefaller
freefaller

Reputation: 19953

The fastest and easiest way I can think of, off the top of my head, would be to create a Console Application in Visual Studio, and place the function in the main class.

Then in the main function call the above function with appropriate output, something like

void main()
{
   string inputStr = "teststring";
   Console.WriteLine(string.Format("{0} = {1}", inputStr, getMD5hash(inputStr)));
   inputStr = "anotherstring";
   Console.WriteLine(string.Format("{0} = {1}", inputStr, getMD5hash(inputStr)));
   Console.ReadKey(); // Pause at the end
}

Upvotes: 1

Related Questions