fsunit


Cutting Edge Unit-Testing with F#

ATTENTION

This project has moved to CodePlex










FsUnit is a library for use with the F# programming language. It is a set of extensions that add a special testing syntax (see below) to your favorite unit-testing framework so that you don't have to learn a new testing framework but you also get to take advantage of everyone's favorite new language.


Syntax

With FsUnit, you can write unit tests like this:

  • One object equals or does not equal another:
    1 |> should equal 1
    

    1 |> should not (equal 2)
  • A List, Seq, or Array instance contains or does not contain a value:
    [1] |> should contain 1
    

    [] |> should not (contain 1)
  • A List or Array instance has a certain length:
    anArray |> should haveLength 4
    
  • A Collection instance has a certain count:
    aCollection |> should haveCount 4
    
  • A function should throw a certain type of exception:
    (fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
    
  • A number of assertions can be created using the be keyword:
    true |> should be True
    

    false |> should not (be True)

    [] |> should be Empty

    [1] |> should not (be Empty)

    "" |> should be EmptyString

    "" |> should be NullOrEmptyString

    null |> should be NullOrEmptyString

    null |> should be Null

    anObj |> should not (be Null)

    anObj |> should be (sameAs anObj)

    anObj |> should not (be sameAs otherObj)

FULL EXAMPLE

Here is a complete set of tests from the http://code.google.com/p/fsunit/source/browse/trunk/Examples/NUnit/'>FsUnit.NUnit examples:

#r "FsUnit.NUnit.dll"

#r "nunit.framework.dll"
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 test.
``when I ask whether it is On it answers true.`` ()=
lightBulb.On |> should be True

[<Test>] member test.
``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 test.
``when I ask whether it is On it answers false.`` ()=
lightBulb.On |> should be False

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

Project Information

Labels:
unittest testing fsharp spec