Book Image

Testing with F#

By : Mikael Lundin
Book Image

Testing with F#

By: Mikael Lundin

Overview of this book

Table of Contents (17 chapters)
Testing with F#
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Tools and frameworks


The built-in tools for unit testing in Visual Studio are not the greatest. To smoothen the process, you need to use some external frameworks and tools.

FsUnit

The tests in this book will mainly focus on FsUnit as the main tool to write unit tests. This is not a test framework like NUnit or xUnit, but more of a helper library to write tests in a more functionally idiomatic way.

Let's start with an example. This function will get the date and time for the Swedish Midsummer in a specified year:

// find date of midsummer eve in Sweden
let midsummerEve year = 
    // get a day in June
    let dateInJune day = new System.DateTime(year, 6, day)
    // is specified date a friday
    let isFriday (date : System.DateTime) = date.DayOfWeek = System.DayOfWeek.Friday
    // find first friday between 19-26 june
    [19..26] |> List.map dateInJune |> List.find isFriday

Instead of writing a test like this:

open NUnit.Framework

// testing using NUnit
[<Test>]
let ``midsummer...