unit-testing The general rules for unit testing for all languages Example of simple unit test in C#


Example

For this example we will test the sum method of a simple calculator.

In this example we will test the application: ApplicationToTest. This one has a class called Calc. This class has a method Sum().

The method Sum() looks like this:

public void Sum(int a, int b)
{
    return a + b;
}

The unit test to test this method looks like this:

[Testclass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        //Arrange
        ApplicationToTest.Calc ClassCalc = new ApplicationToTest.Calc();
        int expectedResult = 5;

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

        //Assert
        Assert.AreEqual(expectedResult, result);
    }
}