unit-testing Guide unit testing in Visual Studio for C# Two methods to create unit tests


Method 1

  • Go to your unit test class in the unit test project
  • Write a unit test
public class UnitTest1
    public void TestMethod1()
        ApplicationToTest.Calc ClassCalc = new ApplicationToTest.Calc();
        int expectedResult = 5;

        int result = ClassCalc.Sum(2,3);

        Assert.AreEqual(expectedResult, result);

Method 2

  • Go the method you want to test
  • Right-click on the method -> Create Unit Tests
  • (Figure 4)

enter image description here

  • Set Test Framework to MSTest
  • Set Test Project to the name of your unit test project
  • Set Output File to the name of the class of the unit tests
  • Set Code for Test Method to one of the options listed which you prefer
  • The other options can be edited but it’s not necessary

(Tip: If you haven’t made a unit tests project yet, you can still use this option. Just set Test Project to and Output File to . It will create the unit test project and it will add the reference of the project to the unit test project)

  • (Figure 5)

enter image description here

  • As you see below it creates the base of the unit test for you to fill in
  • (Figure 6)

enter image description here