Unit Testing C#

 Unit Testing C#


Unit testing allows a program to test individual units of code such as methods, classes, etc. to ensure that they behave as expected. C# provides several frameworks such as NUnit, xUnit, MSTest, etc. to support unit testing. Here's an example code that demonstrates how to use NUnit to test a method:


using NUnit.Framework;


public class MyMath

{

    public int Add(int x, int y)

    {

        return x + y;

    }

}


[TestFixture]

public class MyMathTests

{

    [Test]

    public void Add_WhenCalled_ReturnsSum()

    {

        MyMath math = new MyMath();

        int result = math.Add(2, 3);

        Assert.AreEqual(5, result);

    }

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...