Knows Not Much
Knows Not Much

Reputation: 31546

FSharp FsUnit No test is available in Make sure that test discoverer & executors are registered and platform & framework

OK so a google search reveals 100s if not thousands of links but they are very old threads talking about NUnit and Nuget. I am not using these these tools.

I am writing a F# project with FsUnit and dotnet command line (no visual studio business).

I created a F# project like

dotnet new console -lang "F#" -o CustomerProject  
dotnet add package FsUnit
dotnet add package FsUnit.Xunit
dotnet add package Microsoft.TestPlatform
dotnet add package Microsoft.TestPlatform.TestHost

So my .fsproj file looks like

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Customer.fs" />
    <Compile Include="CustomerTests.fs" />
  </ItemGroup>   
  <ItemGroup>
    <PackageReference Include="FsUnit" Version="5.0.5" />
    <PackageReference Include="FsUnit.Xunit" Version="5.0.5" />
    <PackageReference Include="Microsoft.TestPlatform" Version="17.3.2" />
    <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.3.2" />
  </ItemGroup>    
</Project>

In my project directory I have 2 files

Customer.fs

namespace MyProject.Customer
open System
type Customer = {
  Id : int
  isVip: bool
  Credit: decimal
}
module Domain =
  let getPurchases customer = 
    let purchases = if customer.Id % 2 = 0 then 120M else 80M in 
    (customer, purchases)

  let tryPromoteToVip purchases = 
    let (customer, amount) = purchases in
    if amount > 100M then {customer with isVip = true} else customer

  let increaseCreditIfVip customer = 
    let increase = if customer.isVip then 100M else 50M in
    {customer with Credit = customer.Credit + increase}

  let upgradeCustomer customer = 
    customer 
    |> getPurchases
    |> tryPromoteToVip
    |> increaseCreditIfVip

CustomerTests.fs

module CustomerTests
open Xunit
open FsUnit
open MyProject.Customer
open MyProject.Customer.Domain

module ``When upgrading customer`` = 
  let customerVIP = {Id = 1; isVip = true; Credit = 0.0M}
  let customerSTD = {Id = 2; isVip = false; Credit = 100.0M}
  [<Fact>]
  let ``should give VIP cstomer more credit`` () =
    let expected = {customerVIP with Credit = customerVIP.Credit + 100.0M }
    let actual = upgradeCustomer customerVIP
    actual |> should equal expected

yet when I run dotnet test from the command line I get an error

No test is available in /Users/user/code/fsharp/CustomerProject/bin/Debug/net6.0/CustomerProject.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.

Additionally, path to test adapters can be specified using /TestAdapterPath command. Example  /TestAdapterPath:<pathToCustomAdapters>.

Upvotes: 1

Views: 186

Answers (1)

Brian Berns
Brian Berns

Reputation: 17038

According to this SO answer, you need to add an xUnit runner to your project as well. And according to the xUnit documentation, you have to add xunit.runner.visualstudio, even if you just want to use dotnet test:

dotnet add package xunit.runner.visualstudio

(There's also an xunit.runner.console package on NuGet, but I think it's just for old .NET Framework projects.)

Upvotes: 2

Related Questions