为什么我在基于属性的测试中收到“没有提供参数”错误?

时间:2016-03-21 14:21:57

标签: f# nunit-2.5 fscheck

以下测试失败:

open FsCheck
open FsCheck.NUnit
open NUnit.Framework

let ``Property: double negation equals no negation`` list =
    list = List.rev (List.rev list)

[<Test>]
let ``reversing list two times is equal to not reversing list at all`` list = 
    Check.Quick ``Property: double negation equals no negation``

错误:

  

消息:未提供任何参数

我认为FsCheck会在每次测试迭代时为我提供论据。

我引用了以下documentation

2 个答案:

答案 0 :(得分:5)

这里有一个适用于xUnit.net的版本:

open FsCheck
open Xunit

let ``Property: double negation equals no negation`` list =
    list = List.rev (List.rev list)

[<Fact>]
let ``reversing list two times is equal to not reversing list at all`` () = 
    Check.Quick ``Property: double negation equals no negation``

当你以这种方式使用它时,第一个函数是属性,它可以带参数。

[<Fact>] - 带注释的函数不带参数。

该方法存在的问题是Check.Quick如果属性不成立,则不会导致测试失败。它只输出该财产被伪造。如果您希望测试在属性被伪造时失败,则应使用Check.QuickThrowOnFailure

open FsCheck
open Xunit

let ``Property: double negation equals no negation`` list =
    list = List.rev (List.rev list)

[<Fact>]
let ``reversing list two times is equal to not reversing list at all`` () = 
    Check.QuickThrowOnFailure ``Property: double negation equals no negation``

另一个问题是,没有理由以这种冗长的方式写这篇文章。这是编写相同属性的更紧凑的方法:

open FsCheck
open Xunit

[<Fact>]
let ``reversing list two times is equal to not reversing list at all`` () = 
    Check.QuickThrowOnFailure <| fun (l : int list) ->
        l = List.rev (List.rev l)

答案 1 :(得分:1)

Mark的答案很棒,但只是为了澄清NUnit的情况。

FsCheck.NUnit提供PropertyAttribute来装饰参数的装饰测试方法。它没有挂钩到正常的NUnit TestAttribute。所以换句话说,你的例子有一个正常的NUnit测试,它带有一个参数--NUnit无法应对这个问题。采用您希望FsCheck生成值的参数的测试如下所示:

[<Property>]
let ``Property: double negation equals no negation`` list =
    list = List.rev (List.rev list)

另一个选择 - 如果你不想与NUnit和FsCheck.NUnit搏斗,因为马克说它可能非常脆弱,主要是因为NUnit 2非常烦人的插件模型 - 是不能使用FsCheck.NUnit,而不是使用正常的NUnit测试。使用QuickCheckThrowOnFailure通过异常将测试失败从FsCheck发送到NUnit:

[<Test>]
let ``reversing list two times is equal to not reversing list at all`` () = 
    Check.QuickThrowOnFailure ``Property: double negation equals no negation``

你的例子以某种方式混合了这两个选项。