属性 测试 运行 因为单元测试失败了,即使它真的通过了

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

看来我的 属性 测试 运行 作为单元测试失败了,尽管它确实通过了。

代码如下:

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

总结:

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.

我没看错吗?

我错过了什么?

改用Check.QuickThrowOnFailure

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

由于您似乎正试图从像 NUnit 这样的单元测试框架中 运行 属性,因此您应该考虑改为使用 FsCheck 的 Glue 库之一:

这将使您能够使用 [<Property>] 属性编写属性:

[<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

由于 NUnit 的可扩展性较差 API,您可以使用 xUnit.net 而不是 NUnit 来避免很多麻烦。