[[oktatas:programozás:csharp:dotnetcore|< .Net Core]] ====== .Net Core NUnit 4 ====== * **Szerző:** Sallai András * Copyright (c) 2023, Sallai András * Szerkesztve: 2024 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|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 részbe: false Exe net7.0 enable enable false ===== Ü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));