【问题标题】:Using FsCheck, the propertyCheck function is NOT recognized使用 FsCheck,propertyCheck 函数无法识别
【发布时间】:2015-11-29 21:47:27
【问题描述】:

当我尝试构建测试时,无法识别在我的测试方法中引用的“propertyCheck”函数。

我以为propertyChecked是FsCheck框架的核心功能?

我还需要执行什么其他仪式?

module Tests.Units

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

open FsCheck.NUnit
open FsCheck.NUnit.Addin

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``()=
    propertyCheck commutativeProperty |> should equal true

【问题讨论】:

  • 使用 Check.Quick 代替 propertyCheck。
  • 是什么让您想到 FsCheck 中有一个 propertyCheck 函数?
  • 章节:“重构通用代码” :: fsharpforfunandprofit.com/posts/property-based-testing
  • @ScottNimrod 在那篇文章中定义了该函数:“首先,我们将编写一个名为 propertyCheck 的函数...”
  • 啊...谢谢你告诉我。

标签: f# fscheck


【解决方案1】:

正如@Functional_S 在评论中所写,您可以使用Check.Quick,尽管您应该意识到Check.Quick报告测试结果;如果财产证明是可证伪的,它不会“失败”。在单元测试套件中,Check.QuickThrowOnFailure 是更好的选择,因为顾名思义,它会引发失败。

由于您似乎正在尝试从 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 可以省去很多麻烦。

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2013-06-29
    • 2015-08-16
    相关资源
    最近更新 更多