Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:csharp:dotnetcore:nunit

Ez a dokumentum egy előző változata!


< .Net Core

.Net Core NUnit

Szükséges

Bővítmények Visual Studio Code programnak:

  • C# for Visual Studio Code (powered by OmniSharp) Microsoft
  • NuGet Package Manager

Projekt kezdés

mkdir app01
cd app01
dotnet new console
code .

Program fejlesztése

Triangle.cs
class Triangle {
    public double calcArea(double basea, double height) {
        return basea * height / 2;
    }
}

Teszt fejlesztése

TriangleTest.cs
using NUnit.Framework;
 
[TestFixture]
class TriangleTest {
    [Test]
    public void calcAreaTest() {
        var expected = 525.0;
        var triangle = new Triangle();
        var actual = triangle.calcArea(30.0, 35.0);
        Assert.That(actual, Is.EqualTo(expected));        
    }
}

Függőségek

NuGet

Három csomagot kell hivatkozni.

Jelenítsük meg a parancs panelt.

  • F1
  • NuGet Package Manager: Add Package
    • Microsoft.NET.Test.Sdk
    • NUnit
    • NUnit3TestAdapter

dotnet add

dotnet add package Microsoft.NET.Test.Sdk
dotnet add package NUnit
dotnet add package NUnit3TestAdapter

A .csproj fájl

Vegyük fel az app01.csproj fájlban:

<GenerateProgramFile>false</GenerateProgramFile>

Ha jól csináltuk az app01.csproj fájl tartalma:

app01.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-release-20220113-05"/>
    <PackageReference Include="NUnit" Version="3.13.2"/>
    <PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
  </ItemGroup>
</Project>

Teszt futtatása

dotnet test
dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  app01 -> /home/janos/dev/dotnet/app01/bin/Debug/net6.0/app01.dll
Test run for /home/janos/dev/dotnet/app01/bin/Debug/net6.0/app01.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     1, Skipped:     0, Total:     1, Duration: 24 ms - /home/janos/dev/dotnet/app01/bin/Debug/net6.0/app01.dll (net6.0)
oktatas/programozas/csharp/dotnetcore/nunit.1708287728.txt.gz · Utolsó módosítás: 2024/02/18 21:22 szerkesztette: admin