📎 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.UnitTestsorProject.Business.FunctionalTests. - In the example, the main project contains a
Calculatorclass with aDividemethod, and the test project is namedNUnitProject.UnitTests. - The article says to install
NUnit,NUnit3TestAdapter, andMicrosoft.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, whileOneTimeSetupruns once before all test cases.TearDownandOneTimeTearDownrun 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.
