How do I apply unit testing to C# function which requires user input dynamically?

Create an interface and pass in the interface to receive text. Then, in your unit test, pass in a mock interface that automatically returns some result.

Edit for code details:

public interface IUserInput{
    string GetInput();
}

public static int Get_Commands(IUserInput input){
    do{
       string noOfCommands = input.GetInput();
       // Rest of code here
    }
 }

public class Something : IUserInput{
     public string GetInput(){
           return Console.ReadLine().Trim();
     }
 }

 // Unit Test
 private class FakeUserInput : IUserInput{
      public string GetInput(){
           return "ABC_123";
      }
 }
 public void TestThisCode(){
    GetCommands(new FakeUserInput());
 }

Two essential things:

  1. Console.ReadLine is an external dependency and should be provided to your method in some way (preferably via dependency injection)
  2. Console.ReadLine uses TextReader base class under the hood, and that's what should be provided

So, what your method needs is dependency to TextReader (you could abstract it even more with your custom interface, but for purpose of testing it is enough):

 public static int Get_Commands(TextReader reader)
 {
     // ... use reader instead of Console
 }

Now, in real application you invoke Get_Commands using real console:

    int commandsNumber = Get_Commands(Console.In);

In your unit test, you create fake input using for example StringReader class:

[Test]
public void Get_Commands_ReturnsCorrectNumberOfCommands()
{
   const string InputString = 
       "150" + Environment.NewLine + 
       "10" + Environment.NewLine;
   var stringReader = new StringReader(InputString);

   var actualCommandsNumber = MyClass.Get_Commands(stringReader);

   Assert.That(actualCommandsNumber, Is.EqualTo(10));
}

You can use Console.SetIn() and Console.SetOut() to define the input and output. Use StringReader to define the test's input, and StringWriter to capture the output.

You can see my blog post on the subject, for a more complete explanation and example: http://www.softwareandi.com/2012/02/how-to-write-automated-tests-for.html