如何在Linux上测试F#

How to test F# on linux

您如何在Linux上实际测试F#?
经过许多小时的尝试后,我无法打开我可以打开的每个atom / VScode终端,但仍未取得任何进展。
甚至官方指南也只是挥手致意,简要介绍了一些框架,仅此而已。

简单的问题描述

我有一个hello world应用程序,它在VScode中使用mono和ionide打开了一个功能。因此:

1
2
module Please =
    let itSpeaks number ="hello"

我已经使用paket下载了FsCheck,FsCheck.xunit,xunit,unqoute,xunit.runners,并添加了对" .fsproj"的引用。我已经创建了一个测试。可以说:

1
2
3
4
5
6
7
8
9
10
11
open Xunit.Extensions
open Swensen.Unquote

module Test =

    [<Theory>]
    [<InlineData(-1)>]
    let ``it actually speaks`` (number: int) =
        let actual : string = Please.itSpeaks number
        let expected ="hello"
        test <@ expected = actual @>

现在怎么办?

其他:我会选择原子解决方案或nUnit,或者至少是我朝着正确方向发展的信号。

谢谢!


请注意使用离子水,皮革和假货。您的数据包依存关系可以包括以下内容:

1
2
3
4
// avoid new netcore dependencies for now
nuget NUnit = 3.5.0

nuget NUnit.Runners

希望您知道如何添加对项目的引用。我使用fsproj文件,并在现有的ItemGroup下面供参考,包括:

1
2
3
4
5
6
7
8
9
10
11
12
<Choose>
  <When Condition=" '$(Configuration)'=='Debug'">
    <ItemGroup>
      <Reference Include="nunit.framework">
        <HintPath>..\\packages\
Unit\\lib\
et45\
unit.framework.dll</HintPath>
      </Reference>
    </ItemGroup>
  </When>
</Choose>

使用伪造品,您有一个build.fsx文件。对我来说,我使用内置的Nunit 3:

1
2
buildDebugReferences
|> Testing.NUnit3.NUnit3 (fun p -> { p with WorkingDir = buildDebugResultDir })

其中" buildDebugReferences"在我的FileIncludes路径中,该路径包含我的库,其构建方式为:

1
let buildDebugReferences = !! buildDebugFilter

在我的代码中,我为框架添加了条件:

1
2
3
4
5
6
7
8
9
10
#if DEBUG
open NUnit.Framework
#endif
...
#if DEBUG
[<TestFixture>]
type InternalTestsNunit () =
    [<Test>]
    static member ``Get Answer`` = Assert.AreEqual(1, 1)
#endif

这一切都在正在构建的主库中。因此,我可以在同一个文件中的代码下方进行测试。我这样做是为了进行简单的单行测试。仅Nunit会将更多复杂的测试放在一个单独的项目中。谢谢你。美好的一天。


鉴于您正在运行VS Code和linux,因此我假设您使用的是.net core。几天前,@ Fyodor Soikin在这个线程上遇到了类似的问题,为我提供了帮助。

如何在dotnet Core上对F#进行单元测试

我没有在任何地方看到此文档,但是对我有用。
通过键入

在C#中设置测试项目

1
dotnet new -t xunittest

它将为C#项目创建一个project.json。这接近您想要的内容,但是缺少F#位。我将该文件与F#项目中的project.json文件进行了比较,然后将相关的F#行复制到C#project.json文件中。更新后的project.json如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
 "version":"1.0.0-*",
 "buildOptions": {
   "debugType":"portable",
   "emitEntryPoint": true,
   "compilerName":"fsc",
   "compile": {
     "includeFiles": [
       "Tests.fs"
      ]
    }
  },
 "dependencies": {
   "System.Runtime.Serialization.Primitives":"4.3.0",
   "xunit":"2.1.0",
   "dotnet-test-xunit":"1.0.0-*",
   "---Name of project to test goes here--": {
      "target":"project"
    }
  },
 "tools": {
   "dotnet-compile-fsc":"1.0.0-preview2.1-*"
  },
 "testRunner":"xunit",
 "frameworks": {
   "netcoreapp1.1": {
     "dependencies": {            
       "Microsoft.NETCore.App": {
         "type":"platform",
         "version":"1.1.0"
        },
       "Microsoft.FSharp.Core.netcore":"1.0.0-alpha-160629"
      },
     "imports": [
       "dotnet5.4",
       "portable-net451+win8"
      ]
    }
  }
}

请注意,如果您的测试项目引用了另一个项目,则必须用正在测试的项目名称替换" ---要测试的项目名称到这里-"。

更新测试项目的project.json文件后,运行:

1
2
dotnet restore
dotnet test

您当然可以从上面的project.json开始,但是我提到了创建C#版本以了解它是如何实现的。