Giter Site home page Giter Site logo

NUnit about blog HOT 1 OPEN

WangShuXian6 avatar WangShuXian6 commented on June 5, 2024
NUnit

from blog.

Comments (1)

WangShuXian6 avatar WangShuXian6 commented on June 5, 2024

示例

,NET 8 / NUnit 3

创建一个新的类库项目 用以测试

dotnet new classlib -n MathLib
cd MathLib

编写你的类库代码**

MathLib\SimpleMath.cs

namespace MathLib;

public static class SimpleMath
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

MathLib\MathLib.csproj 系统生成

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

生成一个 NUnit 测试项目示例

-o后接项目目录名称,示例为 MathLibTest

dotnet new nunit -o MathLibTest

来自 https://docs.nunit.org/articles/nunit/getting-started/installation.html

引用库项目

dotnet add reference ../MathLib/MathLib.csproj

编写测试

MathLibTest\SimpleMathTest.cs

using MathLib;
namespace MathLibTest;

public class SimpleMathTests
{
    [SetUp]
    public void Setup()
    {
    }

    [TestCase(1, 2, 3)]
    public void AddTest(int a, int b, int expectedResult)
    {
        int result = SimpleMath.Add(a, b);
        Assert.That(expectedResult, Is.EqualTo(result));
        Assert.Pass();
    }
}

MathLibTest\GlobalUsings.cs 系统生成

global using NUnit.Framework;

MathLibTest\MathLibTest.csproj 系统生成

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
    <PackageReference Include="NUnit" Version="3.13.3" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
    <PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
    <PackageReference Include="coverlet.collector" Version="6.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MathLib\MathLib.csproj" />
  </ItemGroup>

</Project>

TestC.sln

系统生成

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathLib", "MathLib\MathLib.csproj", "{DC429321-8446-4DEB-A930-55A64C414066}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathLibTest", "MathLibTest\MathLibTest.csproj", "{D9B52C00-E99D-4257-842D-83D88B7E16C1}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{DC429321-8446-4DEB-A930-55A64C414066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{DC429321-8446-4DEB-A930-55A64C414066}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{DC429321-8446-4DEB-A930-55A64C414066}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{DC429321-8446-4DEB-A930-55A64C414066}.Release|Any CPU.Build.0 = Release|Any CPU
		{D9B52C00-E99D-4257-842D-83D88B7E16C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{D9B52C00-E99D-4257-842D-83D88B7E16C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{D9B52C00-E99D-4257-842D-83D88B7E16C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{D9B52C00-E99D-4257-842D-83D88B7E16C1}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {238F8662-73C3-4E72-97F8-438814AB1096}
	EndGlobalSection
EndGlobal

运行测试

dotnet test
D:\project\C#\TestC\MathLibTest\bin\Debug\net8.0\MathLibTest.dll (.NETCoreApp,Version=v8.0)的测试运行
Microsoft (R) 测试执行命令行工具版本 17.8.0-preview-23421-06 (x64)
版权所有 (C) Microsoft Corporation。保留所有权利。

正在启动测试执行,请稍候...
总共 1 个测试文件与指定模式相匹配。

已通过! - 失败:     0,通过:     1,已跳过:     0,总计:     1,持续时间: 21 ms - MathLibTest.dll (net8.0)

dotnet test 默认会运行所有发现的测试。以下是其背后的基本原理:

  1. 测试发现:当你运行 dotnet test 命令时,它首先会使用测试适配器(例如 xUnit, NUnit, MSTest 的适配器)来发现项目中的所有测试。这些适配器知道如何识别和定位各自框架的测试方法。
  2. 编译项目:在运行测试之前,dotnet test 会编译整个解决方案或测试项目,以确保所有的代码都是最新的。
  3. 运行测试:编译完成后,dotnet test 使用相应的测试运行器执行已发现的测试。
  4. 收集和报告:测试运行时,结果会被收集起来。一旦所有测试都运行完成,dotnet test 会在控制台输出一个测试结果的概览,包括成功、失败和跳过的测试数量。
  5. 附加功能dotnet test 还支持其他参数,例如并行测试、过滤特定的测试、生成详细的日志等。

其核心原理在于测试框架的适配器和运行器。适配器负责识别测试,而运行器则执行这些测试。这是一个抽象的流程,使得 .NET Core SDK 能够与多种测试框架(如 xUnit, NUnit, MSTest)兼容。

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.