【问题标题】:Property test ran as unit test fails even though it really passes属性测试作为单元测试运行失败,即使它确实通过了
【发布时间】:2015-11-29 21:50:56
【问题描述】:

我的作为单元测试运行的属性测试似乎失败了,即使它确实通过了。

代码如下:

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 FullName: Tests.Units.When I add two numbers, 结果应该 不依赖于参数顺序

测试结果:失败

结果堆栈跟踪:在 FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y) in d:\GitHub\FsUnit\src\FsUnit.NUnit\FsUnit.fs:44行

在 Tests.Units. 当我添加两个数字时,结果不应该取决于 参数 order()

结果消息:预期:true,但

结果标准输出:好的,通过了 100 次测试。

我读对了吗?

我错过了什么?

【问题讨论】:

    标签: f# nunit fscheck fsunit


    【解决方案1】:

    改用Check.QuickThrowOnFailure

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

    由于您似乎正在尝试从 NUnit 等单元测试框架中运行属性,因此您应该考虑改用其中一个用于 FsCheck 的 Glue 库:

    这将使您能够使用[&lt;Property&gt;] 属性编写属性:

    [<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 可以省去很多麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多