Comments (1)
示例
,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
默认会运行所有发现的测试。以下是其背后的基本原理:
- 测试发现:当你运行
dotnet test
命令时,它首先会使用测试适配器(例如 xUnit, NUnit, MSTest 的适配器)来发现项目中的所有测试。这些适配器知道如何识别和定位各自框架的测试方法。 - 编译项目:在运行测试之前,
dotnet test
会编译整个解决方案或测试项目,以确保所有的代码都是最新的。 - 运行测试:编译完成后,
dotnet test
使用相应的测试运行器执行已发现的测试。 - 收集和报告:测试运行时,结果会被收集起来。一旦所有测试都运行完成,
dotnet test
会在控制台输出一个测试结果的概览,包括成功、失败和跳过的测试数量。 - 附加功能:
dotnet test
还支持其他参数,例如并行测试、过滤特定的测试、生成详细的日志等。
其核心原理在于测试框架的适配器和运行器。适配器负责识别测试,而运行器则执行这些测试。这是一个抽象的流程,使得 .NET Core SDK 能够与多种测试框架(如 xUnit, NUnit, MSTest)兼容。
from blog.
Related Issues (20)
- Unity HOT 1
- GPT Prompt 提示词工程 HOT 2
- MetaGPT 源码分析报告 HOT 2
- ADI 亚德诺半导体 电子电气术语表 Glossary of EE Terms HOT 2
- stanford-agent-生成式代理 - 人类行为的交互式仿真
- Blender HOT 1
- 更便宜的3D MMO服务器:GPGPU
- C++ HOT 1
- Unreal Engine 虚幻引擎 HOT 9
- MMORPG + Unreal 5 架构 HOT 6
- C/C++ 工程化 HOT 12
- Cake(C# Make) HOT 6
- KiCad HOT 3
- DALL-E 3 HOT 1
- 虚幻引擎 表演捕捉 Live Link HOT 1
- Unreal Engine 5 程序化内容生成框架(PCG) HOT 5
- 虚幻引擎 Unreal Engine 5 C++ HOT 14
- 虚幻引擎5 材质系统 Unreal Engine 5 Material HOT 6
- 虚幻引擎5 游戏技能系统(GAS) Unreal Engine 5 Gameplay Ability System HOT 29
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.