我正在尝试 F#,并且有一个可以运行的小项目
dotnet test
,但我想在不安装 Microsoft 的任何非自由软件的情况下完成它。
使用
dotnet
的替代方案似乎都使用非常复杂的命令,例如:
$ mono packages/xunit.runner.console.2.0.0/tools/xunit.console.exe build/test/CommandLine.Tests.dll
作为一个完全的新手,我对这些可能需要如何调整才能在我的情况下工作的提示为零。
我的
HelloWorld.fsproj
包含:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Compile Include="HelloWorld.fs" />
<Compile Include="HelloWorldTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="FsUnit.xUnit" Version="4.0.2" />
</ItemGroup>
</Project>
Mono 和 .NET core 都有 MIT 许可证。现在你没有理由不使用 .NET core,特别是 .NET core 5,它旨在统一 .NET core 和 Mono 的功能。
另一方面,如果您不使用 Visual Studio(商业许可证),您的项目代码中会有不必要的行。
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
如果您仍然对此感兴趣,以下是我过去使用 C# 完成此操作的方法:创建一个名为“test”的新目标,您可以从 MSBuild 调用该目标(适用于带有 Microsoft .NET Framework 的 Windows,或与 Mono 跨平台)。应该翻译成 F# 就好了。
CS项目:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<Target Name="Test" DependsOnTargets="Build">
<Exec Command="$(XunitConsole472Path) "$(TargetPath)"" />
</Target>
<ItemGroup>
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.console" Version="2.5.0" />
</ItemGroup>
</Project>
单元测试:
using Xunit;
public class UnitTest1
{
[Fact]
public void Test1()
{
Assert.True(false);
}
}
命令行:
sh$ msbuild /v:minimal /t:restore
Microsoft (R) Build Engine version 16.10.1 for Mono
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
sh$ msbuild /v:minimal /t:test
Microsoft (R) Build Engine version 16.10.1 for Mono
Copyright (C) Microsoft Corporation. All rights reserved.
mono-command-line -> /home/bradwilson/dev/repro/mono-command-line/bin/Debug/net472/mono-command-line.dll
xUnit.net Console Runner v2.5.0+92f31c4c3a (64-bit .NET Framework 4.7.2, runtime: 4.0.30319.42000)
Discovering: mono-command-line
Discovered: mono-command-line
Starting: mono-command-line
UnitTest1.Test1 [FAIL]
Assert.True() Failure
Expected: True
Actual: False
Stack Trace:
at UnitTest1.Test1 () [0x00001] in <95f79fdc5f284dbf9719f6544e480f5a>:0
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <d636f104d58046fd9b195699bcb1a744>:0
Finished: mono-command-line
=== TEST EXECUTION SUMMARY ===
mono-command-line Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.054s
/home/bradwilson/dev/repro/mono-command-line/mono-command-line.csproj(8,5): error MSB3073: The command "mono /home/bradwilson/.nuget/packages/xunit.runner.console/2.5.0/build/../tools/net472/xunit.console.exe "/home/bradwilson/dev/repro/mono-command-line/bin/Debug/net472/mono-command-line.dll"" exited with code 1.