wasAsked.

About NUnit test cases

EPAM · Role not specified · technical

Published

Year not specified · .NET

Community answers

Posted by wasAsked editorial collection

NUnit is a widely used unit-testing framework for .NET. A test class is a plain class containing methods marked with [Test]; the test runner discovers them, runs each one in isolation and reports pass or fail based on assertions. Core building blocks: - [TestFixture] (optional in modern NUnit) marks a class that contains tests. - [Test] marks a single test method. - [TestCase(...)] runs the same test with different inputs, which avoids copy-pasted tests. - [SetUp] / [TearDown] run before and after every test; [OneTimeSetUp] / [OneTimeTearDown] run once per fixture. - Assert.That(actual, Is.EqualTo(expected)) is the constraint-based assertion style NUnit recommends. Example: public class PriceCalculatorTests { private PriceCalculator _calculator = null!; [SetUp] public void SetUp() => _calculator = new PriceCalculator(); [TestCase(100, 10, 90)] [TestCase(50, 0, 50)] public void Applies_discount(decimal price, decimal percent, decimal expected) { var result = _calculator.Apply(price, percent); Assert.That(result, Is.EqualTo(expected)); } [Test] public void Rejects_negative_discount() => Assert.Throws<ArgumentOutOfRangeException>(() => _calculator.Apply(100, -5)); } Good practices interviewers look for: - Follow Arrange-Act-Assert so each test reads clearly. - Test one behaviour per test and name it after that behaviour. - Keep tests independent: no shared mutable state, no reliance on execution order. - Replace slow or external dependencies (database, HTTP, clock) with fakes or mocks such as Moq or NSubstitute. - Run tests in CI on every pull request with "dotnet test". How NUnit compares with xUnit and MSTest: all three are mature. xUnit creates a new class instance per test and uses constructor/IDisposable instead of [SetUp]/[TearDown]; NUnit has richer attributes (TestCaseSource, Category, Retry) and the constraint model. Choosing one matters less than writing focused, deterministic tests.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics