Junior — Middle
What are test fixtures used for in the development and testing process?
sobes.tech AI
Answer from AI
Test fixtures are used to prepare and clean up the test environment before and after test execution. They allow setting an initial state, such as creating objects, configuring the database, or initializing necessary resources, so that tests are isolated and reproducible. In C#, fixtures are often implemented using methods with [SetUp] and [TearDown] attributes in NUnit or similar frameworks.
Example of using a fixture in NUnit:
[TestFixture]
public class CalculatorTests
{
private Calculator _calculator;
[SetUp]
public void Setup()
{
_calculator = new Calculator();
}
[Test]
public void Add_TwoNumbers_ReturnsSum()
{
var result = _calculator.Add(2, 3);
Assert.AreEqual(5, result);
}
[TearDown]
public void Cleanup()
{
// Cleanup resources if necessary
}
}