< .Net Core

.Net Core NUnit 4

Szerző: Sallai András

Copyright © 2023, Sallai András

Szerkesztve: 2024

Licenc: CC BY-SA 4.0

Web: https://szit.hu

Szükséges

Függőségek telepítése:

dotnet add package nunit
dotnet add package nunit3testadapter
dotnet add package microsoft.net.test.sdk

Projekt fájl

Vegyük fel a .csproj fájlban a <PropertyGroup> részbe:

<GenerateProgramFile>false</GenerateProgramFile>

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <GenerateProgramFile>false</GenerateProgramFile> </PropertyGroup> <ItemGroup> <PackageReference Include="microsoft.net.test.sdk" Version="17.8.0" /> <PackageReference Include="nunit" Version="4.0.1" /> <PackageReference Include="nunit3testadapter" Version="4.5.0" /> </ItemGroup> </Project>

Üres főprogram

namespace app02; public class Program { static void Main(string[] args) { Console.WriteLine("Háromszög"); } }

Tesztelendő kód

namespace app01; class Triangle { public double CalcArea(double tbase, double theight) { if (this == null) { throw new InvalidOperationException("Triangle instance is null."); } return tbase * theight / 2; } }

Teszt

namespace app01; using NUnit.Framework; using NUnit.Framework.Legacy; [TestFixture] class TriangleTest { private Triangle? triangle; [SetUp] public void SetUp() { triangle = new Triangle(); } [Test] public void CalcTriangleTest() { if (triangle == null) { throw new InvalidOperationException("Triangle instance is null."); } double actual = triangle.CalcArea(30, 35); double expected = 525.0; ClassicAssert.AreEqual(actual, expected); } }

A teszt futtatása:

dotnet test

Újabban

//E helyett: ClassicAssert.AreEqual(actual, expected); Assert.That(actual, Is.EqualTo(expected));