简体   繁体   中英

How to test F# on linux

How do you actually test F# on Linux?
After many hours of trying everything short of bruteforcing every atom/VScode terminal that I could open - I haven't made any progress.
Even the official guide just hand-waves, briefly introduces a few frameworks and that's that.

Simple question description

I have a hello world app with one function opened inside VScode with mono and ionide. Thus:

module Please =
    let itSpeaks number = "hello"

I have downloaded FsCheck, FsCheck.xunit, xunit, unqoute, xunit.runners using paket and added references to ".fsproj". I have created a test. Lets say:

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 @>

What now?

Additional: I'd settle for an atom solution, or nUnit, or at least a sign that I'm moving in the right direction.

Thank you!

Given the fact that you are running VS Code and linux, I assume that you are using .net core. @Fyodor Soikin helped me a couple of days ago with a similar question on this thread.

How to unit test F# on dotnet Core

I have not seen this documented anywhere but it worked for me. Setup a test project in C# by typing

dotnet new -t xunittest

It will create a project.json for a C# project. That's close to what you want, but it's missing the F# bits. I diffed that file against a project.json file from an F# project and copied the relevant F# lines into the C# project.json file. The updated project.json looks like:

{
  "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"
      ]
    }
  }
}

Note that if your test project is referencing another project, you will have to replace "---Name of project to tests goes here--" with the name of the project you are testing.

After updating the test project's project.json file, run:

dotnet restore
dotnet test

You could certainly just start with the project.json above, but I mentioned creating the C# version to see how it came about.

Please look at using ionide , paket and fake . Your packet dependencies can include the following:

// avoid new netcore dependencies for now
nuget NUnit = 3.5.0

nuget NUnit.Runners

Hopefully you know how to add a reference to your project. I use fsproj files and have below my existing ItemGroup for Reference Include :

<Choose>
  <When Condition=" '$(Configuration)'=='Debug' ">
    <ItemGroup>
      <Reference Include="nunit.framework">
        <HintPath>..\packages\NUnit\lib\net45\nunit.framework.dll</HintPath>
      </Reference>
    </ItemGroup>
  </When>
</Choose>

with fake you have a build.fsx file. For me I use the Nunit 3 built-in:

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

where "buildDebugReferences" is in my FileIncludes path to my library built as:

let buildDebugReferences = !! buildDebugFilter

In my code I put conditionals for the framework:

#if DEBUG
open NUnit.Framework
#endif
...
#if DEBUG
[<TestFixture>]
type InternalTestsNunit () =
    [<Test>]
    static member ``Get Answer`` = Assert.AreEqual(1, 1)
#endif

This is all in the primary library being built. So I get to have my tests right below my code in the same file. I do this for simple one line tests. More tests that are complicated get put in a separate project just for Nunit. Thank you. Good day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM