unit-testingf#tddfscheckproperty-testing

Should property tests run with unit tests when using the RGR methodology?


Should property tests run with unit tests when using the RGR methodology?

RGR: Red -> Green -> Refactor

I noticed that a unit test that I have executes in 18ms.

However, my property test for the same method takes 215ms.

module Tests.Units

open FsUnit
open NUnit.Framework
open NUnit.Core.Extensibility

open FsCheck.NUnit
open FsCheck.NUnit.Addin
open FsCheck

let add x y = (x + y)

let commutativeProperty x y = 
    let result1 = add x y
    let result2 = add y x // reversed params
    result1 = result2

[<Test>]
let ``When I add two numbers, the result should not depend on parameter order``()=
    Check.Quick commutativeProperty

[<Test>]
let ``add two numbers`` () =
    add 2 3 |> should equal (5)

So my property test takes a quarter of a second to execute.

In addition, this is just one simple property test.

What is an effective method for running property tests?

Just check-ins?


Solution

  • With default settings, each FsCheck property runs 100 times, so it's not surprising that it's slower. Notice, though, that it isn't 100 times slower.

    I often use the Red/Green/Refactor process when writing property tests (for the targeted function), and find that it works well.

    It's slower than when doing TDD in C# (also because the F# compiler is slower than the C# compiler). On the other hand, the F# type system is much more expressive, so I also find that I rely more on the type system than I would in C#. This means that I need to write fewer tests.

    All in all, I find that the combination of F# and FsCheck a net win over C# and plain unit testing.