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