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

The false security of code coverage


When doing test automation, you can use a tool that will tell you how large a part of your code is covered by tests. This may seem like a good idea at first glance, but does have some hidden dangers.

The following image shows the code coverage tool, NCover:

The code coverage report will show you what code was traversed and how many times, but it's a false assumption that the report will tell you what was tested. How many times is it required that a test pass a line of code in order to call it covered? Consider the following example:

open NUnit.Framework
open FsUnit

// System Under Test
let rec fibonacci = function
    | n when n < 2 -> 1
    | n -> fibonacci (n - 2) + fibonacci (n - 1)

let fibonacciSeq x = [0..(x - 1)] |> List.map fibonacci

[<Test>]
let returns_correct_result () = 
    fibonacciSeq 5 |> should equal [1; 1; 2; 3; 5]

The test is written only to traverse all the code in one go. It doesn't have a clear purpose of what it...