Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to unit test F# projects in .net core?

I am trying to create an F# unit test project that runs in .net core.

dotnet new -t xunittest

will create an xunit test project for C#, but no such equivalent exists for F#.

I tried modifying the project.json and test file that get output from the C# "dotnet new" shown above, and add the bits I thought would be needed to make it run with F#. It didn't work. It builds and even "runs" a test after typing "dotnet test", but the test ALWAYS passes even when it shouldn't.

Am I missing something with the setup, or is there another (perhaps completely different) option I can pursue in order to create an F# test project? Again, I am specifically focused on making it work with .net core.

project.json

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

Tests.fs

module Tests

open Xunit

    [<Fact>]
    let ``Should Fail`` =        
        Assert.False true 

Output from dotnet test

xUnit.net .NET CLI test runner (64-bit .NET Core win10-x64)
  Discovering: FSTests
  Discovered:  FSTests
=== TEST EXECUTION SUMMARY ===
   FSTests.dll  Total: 0
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
like image 731
Doug Ferguson Avatar asked Jan 31 '17 21:01

Doug Ferguson


Video Answer


1 Answers

Your test is not a function, but a value. Compiled to IL as a property or field (depending on reasons). xUnit looks for methods to execute, it skips properties and fields (and rightly so: how would it execute them?)

Why is it not a function? Because it doesn't have a parameter. It's an F# thing: if you have parameters, you're a function. Otherwise - value.

Just give it a parameter, it will become a function. Don't have any meaningful parameters to add? Add a unit one - then it'll be compiled to IL as a parameterless static method.

[<Fact>]
let ``Should Fail`` () =        
    Assert.False true 
like image 125
Fyodor Soikin Avatar answered Oct 23 '22 03:10

Fyodor Soikin