FsUnit


What is FsUnitTyped?

FsUnitTyped is a statically typed set of FsUnit operators that makes unit-testing with FsUnit even more safe and enjoyable (Available only for NUnit).

No more untyped constrains and tests like

1: 
1 |> should equal "1"

FsUnitTyped from NuGet

The FsUnit.Typed is part of FsUnit package for NUnit and can be installed from NuGet.

FsUnitTyped with Paket

FsUnitTyped supports lightweight usage scenario with Paket.

In the case when you do not want to add dependency on FsUnit package to your project, you can add reference to FsUnitTyped.fs file and NUnit package.

Example of paket.dependencies file:

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
source https://nuget.org/api/v2

nuget FSharp.Core
github fsprojects/FsUnit src/FsUnit.NUnit/FsUnitTyped.fs

group Test
    source https://nuget.org/api/v2
    nuget NUnit.Console
    nuget NUnit

Example of paket.reference file for test projects:

1: 
2: 
3: 
File:FsUnitTyped.fs
group Test
    NUnit

Syntax

With FsUnitTyped, you can write unit tests like this:

1: 
2: 
open NUnit.Framework
open FsUnitTyped

One object equals or does not equal another:

1: 
2: 
1 |> shouldEqual 1
1 |> shouldNotEqual 2

One comparable value greater or smaller than another:

1: 
2: 
11 |> shouldBeGreaterThan 10
10 |> shouldBeSmallerThan 11

A string contains specified substring:

1: 
"ships" |> shouldContainText "hip"

A List, Seq, or Array instance contains, does not contain a value or empty:

1: 
2: 
3: 
[1] |> shouldContain 1
[] |> shouldNotContain 1
[] |> shouldBeEmpty

A List or Array instance has a certain length:

1: 
[|1;2;3;4|] |> shouldHaveLength 4

A function should throw a certain type of exception:

1: 
(fun _ -> failwith "BOOM!") |> shouldFail<System.Exception>

A function should fail

1: 
shouldFail (fun _ -> 5/0 |> ignore)
namespace NUnit
namespace NUnit.Framework
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
namespace System
Multiple items
type Exception =
  new : unit -> Exception + 2 overloads
  member Data : IDictionary
  member GetBaseException : unit -> Exception
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member GetType : unit -> Type
  member HResult : int with get, set
  member HelpLink : string with get, set
  member InnerException : Exception
  member Message : string
  member Source : string with get, set
  ...

Full name: System.Exception

--------------------
System.Exception() : unit
System.Exception(message: string) : unit
System.Exception(message: string, innerException: exn) : unit
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Fork me on GitHub