在Mac Os上快速创建xUnit单元测试程序。xUnit的官方说明文档应该是在windows环境下的,按照里面说的步骤操作会有一些问题。这里记录一下自己再Mac 上的操作。
- 安装运行环境
下载SDK并安装。
安装完成后,检查一下版本,输出正确则表示安装成功。
dotnet --version
2.1.300
- 创建工程
makedir MyUnitTests
cd MyUnitTest
dotnet new xunitThe template "xUnit Test Project" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on /Users/you/workspace/MyUnitTests/MyUnitTests.csproj...
Restoring packages for /Users/you/workspace/MyUnitTests/MyUnitTests.csproj...
Restore completed in 71.73 ms for /Users/you/workspace/MyUnitTests/MyUnitTests.csproj.
Generating MSBuild file /Users/you/workspace/MyUnitTests/obj/MyUnitTests.csproj.nuget.g.props.
Generating MSBuild file /Users/you/workspace/MyUnitTests/obj/MyUnitTests.csproj.nuget.g.targets.
Restore completed in 570.86 ms for /Users/you/workspace/MyUnitTests/MyUnitTests.csproj.
该命令创建了如下文件:
MyUnitTests.csproj UnitTest1.cs bin obj
其中 MyUnitTests.csproj 是项目工程文件。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1" />
</ItemGroup>
</Project>
UnitTest1.cs是默认创建的单元测试类,我们可自行修改或删除。
UnitTest1.cs的内容为:
using System;
using Xunit;
namespace MyUnitTests
{
public class Class1
{
[Fact]
public void PassingTest()
{
Assert.Equal(4, Add(2, 2));
}
[Fact]
public void FailingTest()
{
Assert.Equal(5, Add(2, 2));
}
int Add(int x, int y)
{
return x + y;
}
}
}
- 加载依赖项目并编译
dotnet restore
Restore completed in 48.93 ms for /Users/you/workspace/MyUnitTests/MyUnitTests.csproj.
Restore completed in 21.57 ms for /Users/you/workspace/MyUnitTests/MyUnitTests.csproj.
dotnet build
Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.Restore completed in 22.97 ms for /Users/you/workspace/MyUnitTests/MyUnitTests.csproj.
Restore completed in 49.41 ms for /Users/ldong/workspace/MyUnitTests/MyUnitTests.csproj.
MyUnitTests -> /Users/you/workspace/MyUnitTests/bin/Debug/netcoreapp2.1/MyUnitTests.dllBuild succeeded.
0 Warning(s)
0 Error(s)
此时我们看到编译成功了。
- 运行测试
dotnet test
Build started, please wait...
Build completed.Test run for /Users/you/workspace/MyUnitTests/bin/Debug/netcoreapp2.1/MyUnitTests.dll(.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 15.7.0
Copyright (c) Microsoft Corporation. All rights reserved.Starting test execution, please wait...
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.5573 Seconds
最终测试运行成功。
可能遇到的问题
测试无法运行
No test is available in /Users/MyUnitTests.dll. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk" and framework version settings are appropriate and try again.
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
请检查工程文件中的TargetFramwork,因为我们是Mac环境,请确保它是netcoreapp2.x,本例中我的环境是netcoreapp2.0。
其他问题请参照下面的链接:
on Mac Os does not discover unit tests (xunit)
Set up unit testing with xUnit in .NET Core
参考链接:
[1] https://xunit.github.io/docs/getting-started-dotnet-core "xUnit"
[2] https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test
网友评论