.Net Core on Mac 创建xUnit单元测试

在Mac Os上快速创建xUnit单元测试程序。xUnit的官方说明文档应该是在windows环境下的,按照里面说的步骤操作会有一些问题。这里记录一下自己再Mac 上的操作。

  1. 安装运行环境
    下载SDK并安装。
    安装完成后,检查一下版本,输出正确则表示安装成功。

dotnet --version
2.1.300

  1. 创建工程

makedir MyUnitTests
cd MyUnitTest
dotnet new xunit

The 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;
        }
    }
}

  1. 加载依赖项目并编译

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.dll

Build succeeded.
0 Warning(s)
0 Error(s)

此时我们看到编译成功了。

  1. 运行测试

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,724评论 0 3
  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622阅读 1,493评论 0 2
  • 在这个世界上,我们在天涯在海角,每一个人都在忙着各自不同的工作,都在参与着各自不同的人生,都在走着各自不同的人生道...
    诙老斯阅读 457评论 2 8
  • 亲爱的女儿婷婷: 岁月如梭光阴似箭转眼间就快到妳十岁生日,学校的这次“十岁生日”活动让我想起,妳来到我身边发...
    兰亭迷阅读 288评论 0 0
  • 如果能在夏天的傍晚时,急匆匆扒掉一碗饭后换上漂亮的连衣裙,随后去见一个喜欢的人。和他一起走在安静的巷子里,或是热闹...
    吕三岁同学阅读 742评论 3 11