Unit testing is a crucial part of software development that helps ensure your code behaves as expected. Using frameworks like NUnit or MSTest in C#, you can create and run tests to verify the functionality of your code. Below is an example demonstrating how to perform unit testing using these frameworks.
NUnit is a popular unit-testing framework for C#. Here's a simple example:
using NUnit.Framework;
[TestFixture]
public class CalculatorTests
{
private Calculator _calculator;
[SetUp]
public void Setup()
{
_calculator = new Calculator();
}
[Test]
public void Add_WhenCalled_ReturnsSumOfArguments()
{
var result = _calculator.Add(2, 3);
Assert.AreEqual(5, result);
}
}
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
MSTest is another testing framework provided by Microsoft. Here's how you would write a similar test using MSTest:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CalculatorTests
{
private Calculator _calculator;
[TestInitialize]
public void Setup()
{
_calculator = new Calculator();
}
[TestMethod]
public void Add_WhenCalled_ReturnsSumOfArguments()
{
var result = _calculator.Add(2, 3);
Assert.AreEqual(5, result);
}
}
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?