Függőségek telepítése:
dotnet add package nunit dotnet add package nunit3testadapter dotnet add package microsoft.net.test.sdk
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>
namespace app02; public class Program { static void Main(string[] args) { Console.WriteLine("Háromszög"); } }
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; } }
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
//E helyett: ClassicAssert.AreEqual(actual, expected); Assert.That(actual, Is.EqualTo(expected));