When there is the need for change, you want to be confident that you don't break anything. To be more precise: You don't want to break any behavior (Hunt, Thomas, Pragmatic Unit Testing in C# with NUnit, p. 31).
With unit testing in place you can do changes with much more confidence, because they would (provided they are programmed properly) capture changes in behavior. That's the benefit of unit tests.
It would be difficult to make unit tests for the class you gave as an example, because unit tests also requires a certain structure of the code under test. One reason I see is that the class is doing too much. Any unit tests you will apply on that class will be quite brittle. Minor change may make your unit tests blow up and you will end up wasting much time with fixing problems in your test code instead of your production code.
To reap the benefits of unit tests requires to change the production code. Just applying unit tests principles, without considering this will not give you the positive unit testing experience.
How to get the positive unit testing experience? Be openminded for it and learn.
If you're asking about unit testing techniques, get a book. Perhaps this or this.
If you're wanting to test code that calls SharePoint objects, you have to talk about tools. You have to fake these out using either Typemock Isolator or Moles. The SharePoint object model is full of concrete, non-inheritable objects.
it's not right approach. You shouldn't communicate with Console in unit tests. Just extract your function which works with input parameters and test this function.
like this:
in YourExtractedClass:
public string GetMessage(string input)
{
var result = string.Empty;
switch (input)
{
case "1":
result = "Enter Account Number: ";
break;
case "2":
result = "Hello World!";
break;
default:
break;
}
return result;
}
....
In your Test class for YourExtractedClass
[Test]
public void GetMessage_Input1_ReturnEnterAccountNumberMessage()
{
var result = GetMessage("1");
var expected = "Enter Account Number: ";
Assert.That(result == expected);
}
[Test]
public void GetMessage_Input2_ReturnHelloWorldMessage()
{
var result = GetMessage("1");
var expected = "Hello World!";
Assert.That(result == expected);
}
And one more thing: it's better to move you strings ("Enter Account Number" etc) to one place (fro example to some Constants class). Don't repeat yourself!
Tests will help, if you need to make changes.
According to Feathers (Feathers, Working Effectively with Legacy Code, p. 3) there are four reasons for changes:
When there is the need for change, you want to be confident that you don't break anything. To be more precise: You don't want to break any behavior (Hunt, Thomas, Pragmatic Unit Testing in C# with NUnit, p. 31).
With unit testing in place you can do changes with much more confidence, because they would (provided they are programmed properly) capture changes in behavior. That's the benefit of unit tests.
It would be difficult to make unit tests for the class you gave as an example, because unit tests also requires a certain structure of the code under test. One reason I see is that the class is doing too much. Any unit tests you will apply on that class will be quite brittle. Minor change may make your unit tests blow up and you will end up wasting much time with fixing problems in your test code instead of your production code.
To reap the benefits of unit tests requires to change the production code. Just applying unit tests principles, without considering this will not give you the positive unit testing experience.
How to get the positive unit testing experience? Be openminded for it and learn.
I would recommend you Working Effectively with Legacy Code for an existing code basis (as that piece of code you gave above). For an easy kick start into unit testing try Pragmatic Unit Testing in C# with NUnit. The real eye opener for me was xUnit Test Patterns: Refactoring Test Code.
Good luck in you journey!
Your question is really vague.
If you're asking about unit testing techniques, get a book. Perhaps this or this.
If you're wanting to test code that calls SharePoint objects, you have to talk about tools. You have to fake these out using either Typemock Isolator or Moles. The SharePoint object model is full of concrete, non-inheritable objects.
it's not right approach. You shouldn't communicate with Console in unit tests. Just extract your function which works with input parameters and test this function.
like this:
in YourExtractedClass:
....
In your Test class for YourExtractedClass
And one more thing: it's better to move you strings ("Enter Account Number" etc) to one place (fro example to some Constants class). Don't repeat yourself!
read good books about unit testing:
The Art of Unit Testing: With Examples in .Net
Pragmatic Unit Testing in C# with NUnit, 2nd Edition
xUnit Test Patterns: Refactoring Test Code