Testing in .NET Core with Nunit

(related tool on how to upgrade a project to .NET 8)

Setup a project for testing

Agregar > nuevo proyecto testing image 1

Proyecto de prueba NUnit testing image 2

Ponerle como nombre `.Tests

Esto deberia crear un proyecto nuevo con un test autogenerado testing image 3

Comprobar los following Nuggets

NUnit
NUnit.Analyzers
NUnit3TestAdapter
Moq
FluentAssertions
FluentAssertions.Analyzers

// para testear ApplicationDbContext
Microsoft.EntityFrameworkCore.InMemory
Moq.EntityFrameworkCore

Como vamos a testear el proyecto original desde el proyecto nuevo .Tests, necesitamos añadir una referencia al mismo desde el proyecto .Tests

click derecho en proyecto .Tests > agregar > Referencia del proyecto…

testing image 4

Seleccionar el proyecto a testear > aceptar

testing image 5

Naming conventions

Name testing projects as [ProjectUnderTest].Tests Name testing classes as [ClassNameUnderTest]Test

Name testing functions different styles:

  1. start with should and write the general idea void ShouldAddTwoNumbers
  2. start with the name of the function under test and _ and what should it do void Sum_ShouldAddTwoNumbers
  3. UnitUnderTest_Scenario_ExpectedOutcome ParsePort_COM1_Returns1