Sobes.tech
Junior — Middle

Can you list the main attributes used in test scenarios?

sobes.tech AI

Answer from AI

The following main attributes are often used in C# test scenarios:

  • [TestMethod] — marks a method as a test (used in MSTest).
  • [Test] — similar attribute for NUnit.
  • [Fact] and [Theory] — attributes from xUnit, where [Fact] is a simple test, and [Theory] is parameterized.
  • [SetUp] or [TestInitialize] — method that runs before each test to prepare the environment.
  • [TearDown] or [TestCleanup] — method that runs after each test for cleanup.
  • [ClassInitialize] and [ClassCleanup] — methods for setup and cleanup at the class level.
  • [Ignore] or [Skip] — to skip a test.

Example with MSTest:

[TestClass]
public class CalculatorTests
{
    [TestInitialize]
    public void Setup()
    {
        // Setup code
    }

    [TestMethod]
    public void Add_TwoNumbers_ReturnsSum()
    {
        var calc = new Calculator();
        var result = calc.Add(2, 3);
        Assert.AreEqual(5, result);
    }

    [TestCleanup]
    public void Cleanup()
    {
        // Cleanup after test
    }
}

These attributes help structure tests and manage their execution.