↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Introduction to Unit Testing With NUnit in C#

The article introduces unit testing in C# with NUnit and explains the basic structure of a unit test through Arrange, Act, Assert. It also covers naming conventions for test methods and test projects, plus how to set up a simple test project for a Calculator class.

Reading notes
#

  • Unit tests are automated tests written and run by the developer to check that a small part of an application behaves as intended.
  • A unit test typically follows Arrange, Act, Assert, with the expected value defined in Arrange to keep Assert simple.
  • A common naming pattern is [GivenX]_When[Y]_Then[Z].
  • Test projects are usually separated by business project, with names like Project.Business.UnitTests or Project.Business.FunctionalTests.
  • In the example, the main project contains a Calculator class with a Divide method, and the test project is named NUnitProject.UnitTests.
  • The article says to install NUnit, NUnit3TestAdapter, and Microsoft.NET.Test.Sdk.
  • A test method is marked with the [Test] attribute.
  • Tests can be run in Visual Studio or with dotnet test.
  • [TestCase] lets the same test run with different constant inputs.
  • [TestCaseSource] accepts a static method or property that provides test values.
  • [SetUp] runs before each test case, while OneTimeSetup runs once before all test cases.
  • TearDown and OneTimeTearDown run after tests.
  • The article describes metadata attributes such as [Author], [Description], [MaxTime], and [Category].
  • [Category] can be used to group tests and run only the tests for a specific feature.
  • The conclusion says NUnit is a popular .NET testing framework with tools for unit tests and metadata.