Step-1: Open the visual studio (here I used VS-2015) and navigate to new project -> Visual C# -> Test. Select Unite Test Project and give any meaning full name for your project and select preferred and Press OK

Image-1 : Adding test project.
Step-2: That’s it, you successfully created your first test project in visual studio. Now you need to understand few of the attributes which are decorated for test project.
- [TestClass] – It’s test attribute which turns a class into test class.
- [TestMethod] – It’s test attribute which turns any method into test method, from which our testing starts.
There are several other attributes for test project but we will keep simple for this articles with above attributes and remaining things I will post new article where I will explain in details on the same.

Image-2: Test attributes and overview.
Step-3: If you see the solution explorer, we can find a reference with name UnitTestFramework which is core for unit test project with in which all classes and attributes are covered in order to perform the unit testing.

Image-3: Test Project structure.
Step-4: Now we will work on an example which shows usage of test cases in real time. To do this I added new class called DemoClass, under that I added a static method with accepts an integer value and returns string value. Inside implementation has list of integers and filters for given value and returns the string value.

Image-4: DemoClass to test.
Step-5: Now we will see an example of testing scenarios.
Passing scenario:
Here I’m calling that static method in test method by passing value as 10 and I’m expecting “10” as return. In this case test will pass because sequence has 10 and returns “10”. I used meaningful name for this method as public void GetMyValue_ReturnedValueInString_ShouldReturnValue(), your test methods should summarize what it performs by using very meaningful names.
To run test – open test explores view and press run test. You can also debug the specific tests by putting break points and pressing debug test.

Image-5: Pass Scenario.
Failing scenario:
Here I created a failing method where I used input as 100, which not belongs to sequence and will get an exception. Which is failing scenario for our case.
Run the test and we will get an exception as Sequence not belongs something.

Image-6: Failing scenario.
Pass scenario for failed one:
To fix the above scenario we should alter our method in DemoClass, by just modifying as SingleOrDefault which returns default value (in our case 0) and such that we can overcome the failing scenario.

Image-7: Pass scenario for failed one.
- Once you run the test for all test cases in test explorer, you can see a green ticks for each test methods which ensures the methods are ready to use in real time.

Image-8: Running all scenarios.
Conclusion: I shown basic way of testing your methods with simple demo example and we can test complex things like repositories, controllers etc. This articles gear ups for beginners and then they explore the things one by one in unit testing project.
