Search code examples
f#nunitfscheckfsunit

Property test ran as unit test fails even though it really passes


It appears that my property test that's running as a unit test fails even though it really passes.

The code is as follows:

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 |> should equal true

Summary:

Test Name: When I add two numbers, the result should not depend on parameter order

Test FullName: Tests.Units.When I add two numbers, the result should not depend on parameter order

Test Outcome: Failed

Result StackTrace: at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y) in d:\GitHub\FsUnit\src\FsUnit.NUnit\FsUnit.fs:line 44

at Tests.Units.When I add two numbers, the result should not depend on parameter order()

Result Message: Expected: true, but was

Result StandardOutput: Ok, passed 100 tests.

Am I reading this right?

What am I missing?


Solution

  • Use Check.QuickThrowOnFailure instead:

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

    Since it looks like you're attempting to run properties from within a unit testing framework like NUnit, you should consider to instead use one of the Glue Libraries for FsCheck:

    This would enable you to write properties using the [<Property>] attribute:

    [<Property>]
    let ``When I add two numbers, the result should not depend on parameter order``x y =
        let result1 = add x y
        let result2 = add y x // reversed params
        result1 = result2
    

    Due to the poor extensibility API for NUnit, you can save yourself a lot of grief using xUnit.net instead of NUnit.