FsUnit


FsUnit for NUnit

The FsUnit library for NUnit can be installed from NuGet:
PM> Install-Package FsUnit
Sample FsUnit tests for NUnit can be installed from NuGet:
PM> Install-Package FsUnit.Sample

If you reference FsUnit from NuGet and want to use pretty-printed F# value types in error messages, you have to add following piece of code to your assembly:

1: 
2: 
3: 
4: 
open FsUnit

type InitMsgUtils() =
    inherit FSharpCustomMessageFormatter()

Euler - Problem 1

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
module ``Project Euler - Problem 1`` =
    open NUnit.Framework
    open FsUnit

    let GetSumOfMultiplesOf3And5 max =
        seq{3..max-1} |> Seq.fold(fun acc number ->
                            (if (number % 3 = 0 || number % 5 = 0) then
                                acc + number else acc)) 0

    [<Test>]
    let ``When getting sum of multiples of 3 and 5 to a max number of 10 it should return a sum of 23`` () =
        GetSumOfMultiplesOf3And5(10) |> should equal 23

LightBulb

 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: 
module ``LightBulb Tests`` =
    open NUnit.Framework
    open FsUnit

    type LightBulb(state) =
        member x.On = state
        override x.ToString() =
            match x.On with
            | true  -> "On"
            | false -> "Off"

    [<TestFixture>]
    type ``Given a LightBulb that has had its state set to true`` ()=
        let lightBulb = new LightBulb(true)

        [<Test>] member x.
         ``when I ask whether it is On it answers true.`` ()=
                lightBulb.On |> should be True

        [<Test>] member x.
         ``when I convert it to a string it becomes "On".`` ()=
                string lightBulb |> should equal "On"

    [<TestFixture>]
    type ``Given a LightBulb that has had its state set to false`` ()=
        let lightBulb = new LightBulb(false)

        [<Test>] member x.
         ``when I ask whether it is On it answers false.`` ()=
                lightBulb.On |> should be False

        [<Test>] member x.
         ``when I convert it to a string it becomes "Off".`` ()=
                string lightBulb |> should equal "Off"

BowlingGame

Thanks to Keith Nicholas and Julian from hubFS for this example!

 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: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
module ``BowlingGame A game of bowling`` =
    open NUnit.Framework
    open FsUnit

    let (|EndOfGame|IncompleteStrike|Strike|Normal|Other|) (l, frame) =
        match l with
        | _ when frame = 11            -> EndOfGame(0)
        | [10;s]                       -> IncompleteStrike(10+s+s)
        | 10::s::n::tail               -> Strike(10+s+n, s::n::tail)
        |  f::s::n::tail when f+s = 10 -> Normal(f+s+n,  n::tail)
        |  f::s::n::tail               -> Normal(f+s,    n::tail)
        | ls                           -> Other(List.fold (+) 0 ls)

    let scoreBowls bowls =
        let rec scoreBowls' frame l current_score =
            let nextframe = scoreBowls' (frame+1)
            match (l, frame) with
            | EndOfGame(score)        -> current_score + score
            | IncompleteStrike(score) -> current_score + score
            | Strike(score, l)        -> nextframe l (current_score + score)
            | Normal(score, l)        -> nextframe l (current_score + score)
            | Other(score)            -> current_score + score
        scoreBowls' 1 bowls 0

    [<Test>]
    let ``with simple scores should get the expected score.`` () =
        scoreBowls [1;2;3] |> should equal 6

    [<Test>]
    let ``with a spare should get the expected score (spare).`` () =
        scoreBowls [2;8;1] |> should equal 12

    [<Test>]
    let ``with a strike should get the expected score (strike).`` () =
        scoreBowls [10;1;2] |> should equal 16

    [<Test>]
    let ``that is perfect should get a score of 300.``() =
        scoreBowls [for i in 1..18 -> 10] |> should equal 300

    [<Test>]
    let ``with spares in the last frame should get the expected score (spare in last frame).`` () =
        scoreBowls ([for i in 1..18 -> 0] @ [2;8;1]) |> should equal 11

    [<Test>]
    let ``with a strike in the last frame should get the expected score (strike in last frame).`` () =
        scoreBowls ([for i in 1..18 -> 0] @ [10;10;1]) |> should equal 21

    [<Test>]
    let ``with double strikes should add the score of the first strike to the score of the second.`` () =
        scoreBowls [10;10;1] |> should equal 33

    [<Test>]
    let ``that looks like an average bowler's game should get the expected score (example game).`` () =
        scoreBowls [1;4;4;5;6;4;5;5;10;0;1;7;3;6;4;10;2;8;6] |> should equal 133

Note: NUnit can also be utilized without specifying a type as in the examples for MbUnit and xUnit

namespace FsUnit
Multiple items
type InitMsgUtils =
  inherit FSharpCustomMessageFormatter
  new : unit -> InitMsgUtils

Full name: NUnit.InitMsgUtils

--------------------
new : unit -> InitMsgUtils
Multiple items
type FSharpCustomMessageFormatter =
  new : unit -> FSharpCustomMessageFormatter

Full name: FsUnit.TopLevelOperators.FSharpCustomMessageFormatter

--------------------
new : unit -> FSharpCustomMessageFormatter
namespace NUnit
namespace NUnit.Framework
val GetSumOfMultiplesOf3And5 : max:int -> int

Full name: NUnit.Project Euler - Problem 1.GetSumOfMultiplesOf3And5
val max : int
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
module Seq

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val acc : int
val number : int
Multiple items
type TestAttribute =
  inherit NUnitAttribute
  new : unit -> TestAttribute
  member ApplyToTest : test:Test -> unit
  member Author : string with get, set
  member BuildFrom : method:IMethodInfo * suite:Test -> TestMethod
  member Description : string with get, set
  member ExpectedResult : obj with get, set
  member HasExpectedResult : bool with get, set
  member TestOf : Type with get, set

Full name: NUnit.Framework.TestAttribute

--------------------
TestAttribute() : unit
val ( When getting sum of multiples of 3 and 5 to a max number of 10 it should return a sum of 23 ) : unit -> unit

Full name: NUnit.Project Euler - Problem 1.( When getting sum of multiples of 3 and 5 to a max number of 10 it should return a sum of 23 )
val should : f:('a -> #Constraints.Constraint) -> x:'a -> y:obj -> unit

Full name: FsUnit.TopLevelOperators.should
val equal : x:'a -> Constraints.EqualConstraint

Full name: FsUnit.TopLevelOperators.equal
Multiple items
type LightBulb =
  new : state:bool -> LightBulb
  override ToString : unit -> string
  member On : bool

Full name: NUnit.LightBulb Tests.LightBulb

--------------------
new : state:bool -> LightBulb
val state : bool
val x : LightBulb
member LightBulb.On : bool

Full name: NUnit.LightBulb Tests.LightBulb.On
override LightBulb.ToString : unit -> string

Full name: NUnit.LightBulb Tests.LightBulb.ToString
property LightBulb.On: bool
Multiple items
type TestFixtureAttribute =
  inherit NUnitAttribute
  new : unit -> TestFixtureAttribute + 1 overload
  member Arguments : obj[] with get, set
  member Author : string with get, set
  member BuildFrom : typeInfo:ITypeInfo -> IEnumerable<TestSuite>
  member Category : string with get, set
  member Description : string with get, set
  member Explicit : bool with get, set
  member Ignore : string with get, set
  member IgnoreReason : string with get, set
  member Properties : IPropertyBag with get, set
  ...

Full name: NUnit.Framework.TestFixtureAttribute

--------------------
TestFixtureAttribute() : unit
TestFixtureAttribute([<System.ParamArray>] arguments: obj []) : unit
val lightBulb : LightBulb
val x : Given a LightBulb that has had its state set to true
member Given a LightBulb that has had its state set to true.( when I ask whether it is On it answers true. ) : unit -> unit

Full name: NUnit.LightBulb Tests.Given a LightBulb that has had its state set to true.( when I ask whether it is On it answers true. )
val be : ('a -> 'a)

Full name: FsUnit.TopLevelOperators.be
val True : Constraints.TrueConstraint

Full name: FsUnit.TopLevelOperators.True
member Given a LightBulb that has had its state set to true.( when I convert it to a string it becomes "On". ) : unit -> unit

Full name: NUnit.LightBulb Tests.Given a LightBulb that has had its state set to true.( when I convert it to a string it becomes "On". )
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val x : Given a LightBulb that has had its state set to false
member Given a LightBulb that has had its state set to false.( when I ask whether it is On it answers false. ) : unit -> unit

Full name: NUnit.LightBulb Tests.Given a LightBulb that has had its state set to false.( when I ask whether it is On it answers false. )
val False : Constraints.FalseConstraint

Full name: FsUnit.TopLevelOperators.False
member Given a LightBulb that has had its state set to false.( when I convert it to a string it becomes "Off". ) : unit -> unit

Full name: NUnit.LightBulb Tests.Given a LightBulb that has had its state set to false.( when I convert it to a string it becomes "Off". )
val l : int list
val frame : int
val s : int
val n : int
val tail : int list
val f : int
val ls : int list
Multiple items
type List =
  new : unit -> List
  static member Map : actual:ICollection -> ListMapper

Full name: NUnit.Framework.List

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>

--------------------
List() : unit
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val scoreBowls : bowls:int list -> int

Full name: NUnit.BowlingGame A game of bowling.scoreBowls
val bowls : int list
val scoreBowls' : (int -> int list -> int -> int)
val current_score : int
val nextframe : (int list -> int -> int)
active recognizer EndOfGame: int list * int -> Choice<int,int,(int * int list),(int * int list),int>

Full name: NUnit.BowlingGame A game of bowling.( |EndOfGame|IncompleteStrike|Strike|Normal|Other| )
val score : int
active recognizer IncompleteStrike: int list * int -> Choice<int,int,(int * int list),(int * int list),int>

Full name: NUnit.BowlingGame A game of bowling.( |EndOfGame|IncompleteStrike|Strike|Normal|Other| )
active recognizer Strike: int list * int -> Choice<int,int,(int * int list),(int * int list),int>

Full name: NUnit.BowlingGame A game of bowling.( |EndOfGame|IncompleteStrike|Strike|Normal|Other| )
active recognizer Normal: int list * int -> Choice<int,int,(int * int list),(int * int list),int>

Full name: NUnit.BowlingGame A game of bowling.( |EndOfGame|IncompleteStrike|Strike|Normal|Other| )
active recognizer Other: int list * int -> Choice<int,int,(int * int list),(int * int list),int>

Full name: NUnit.BowlingGame A game of bowling.( |EndOfGame|IncompleteStrike|Strike|Normal|Other| )
val ( with simple scores should get the expected score. ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( with simple scores should get the expected score. )
val ( with a spare should get the expected score (spare). ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( with a spare should get the expected score (spare). )
val ( with a strike should get the expected score (strike). ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( with a strike should get the expected score (strike). )
val ( that is perfect should get a score of 300. ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( that is perfect should get a score of 300. )
val i : int
val ( with spares in the last frame should get the expected score (spare in last frame). ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( with spares in the last frame should get the expected score (spare in last frame). )
val ( with a strike in the last frame should get the expected score (strike in last frame). ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( with a strike in the last frame should get the expected score (strike in last frame). )
val ( with double strikes should add the score of the first strike to the score of the second. ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( with double strikes should add the score of the first strike to the score of the second. )
val ( that looks like an average bowler's game should get the expected score (example game). ) : unit -> unit

Full name: NUnit.BowlingGame A game of bowling.( that looks like an average bowler's game should get the expected score (example game). )
Fork me on GitHub