当我预期失败时,基于属性的测试继续通过

时间:2016-03-25 02:00:06

标签: f# property-based-testing

我确定这是微不足道的。但是,以下测试将始终通过:

let transform number =  
    match number % 3, number % 5 with 
    | 0, 0 -> "FizzBuzz" 
    | _, 0 -> "Buzz" 
    | 0, _ -> "Fizz" 
    | _ -> number.ToString()

[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5)) 
                                          |> Arb.fromGen

    Prop.forAll fiveAndThrees <| fun number ->

        let actual = transform number

        let expected = "jbdhjsdhjdsjhsdglsdjlljh"
        expected = actual

我也尝试过:

Check.QuickThrowOnFailure <| (expected = actual)

具体来说,为什么这个测试显然会失败时继续通过?

2 个答案:

答案 0 :(得分:4)

您正在创建该属性,但从未实际检查过该属性。它只是坐在那里,从未执行过一次。

要检查属性,您需要将其传递给Check.*方法之一:

[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5)) 
                                          |> Arb.fromGen

    let myProperty = Prop.forAll fiveAndThrees <| fun number ->

        let actual = transform number

        let expected = "jbdhjsdhjdsjhsdglsdjlljh"
        expected = actual

    Check.QuickThrowOnFailure myProperty

答案 1 :(得分:2)

您还可以使用 FsCheck.Xunit 及其[<Property>]属性:

[<Property(QuietOnSuccess = true)>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
    let fiveAndThrees =
        Arb.generate<int> |> Gen.map ((*) (3 * 5)) |> Arb.fromGen
    Prop.forAll fiveAndThrees <| fun number ->

        let actual = FizzBuzz.transform number

        let expected = "FizzBuzz"
        expected = actual