FsUnit is a stand-alone specification framework for use with the F# programming language. Written itself in F#, it offers a functional alternative to traditional imperative frameworks.
Specification Syntax
With FsUnit, you can write specifications like this:
- One object equals or does not equal another:
1 |> should equal 1 1 |> should not' (equal 2)
["item"] |> should contain "item" ["item"] |> should not' (contain "not item")
personList |> should have 4 "people"
- A function should throw a certain type of exception:
(fun () -> failwith "BOOM!") |> should (raise'<FailureException>)
- Boolean, Null, NullOrEmpty, Empty, and SameAs assertions have their own helpers:
true |> should be True false |> should not' (be True) "" |> should be Empty "a string" |> should not' (be Empty) "" |> should be NullOrEmpty null |> should be NullOrEmpty null |> should be Null anObj |> should not' (be Null) anObj |> should be (SameAs(otherObj))
The specs and spec Keywords
Let FsUnit manage the storage and execution of your specs, use the specs keyword:
specs "My Specs" [
spec "A number should equal itself."
(1 |> should equal 1)
]
printfn "%s" (Results.summary())For more examples of using the spec and specs keywords, see the examples.
FULL EXAMPLE
The full code for a specification in FsUnit looks like this:
#light
#r "FsUnit.dll"
open FsUnit
type LightBulb(state) =
member x.On = state
override x.ToString() =
match x.On with
| true -> "On"
| false -> "Off"
let onSpecs =
let lb = LightBulb(true)
specs "On Specs" [
spec "On should be true."
(lb.On |> should be True)
spec "String representation should equal \"On\"."
(lb.ToString() |> should equal "On")
]
let offSpecs =
let lb = LightBulb(false)
specs "Off Specs" [
spec "Off should be false."
(lb.On |> should be False)
spec "String representation should equal \"Off\"."
(lb.ToString() |> should equal "Off")
]
printfn "%s" (Results.summary())