How do I get helpful assertion error messages for FsUnit?

I am using FsUnit 2.3.2 and I am not happy with the crash messages. See examples below:

[<Test>]
let ``test 1``() =
    [1; 3]
    |> should equal [1;2]

      

... gives me a not very useful message:

Expected and Actual Microsoft.FSharp.Collections.FSharpList`1 [System.Int32]

in FsUnit.TopLevelOperators.should [a, a] (FSharpFunc`2 f, ax, Object y) in d: \ GitHub \ FsUnit \ src \ FsUnit.NUnit \ FsUnit.fs: line 44 on Program.test 1 () in F: \ work \ playground \ fsunit \ fsunit \ Program.fs: line 9

The workaround I found was to use arrays instead of lists:

[<Test>]
let ``test 2``() =
    [|1; 4|]
    |> should equal [|1;2|]

      

... gives

Expected and Actual are System.Int32 [2]
  Values ​​differ by index [1]
  Expected: 2
  But was: 4

The second problem is that I have a specific ADT

type MyT = 
    A of int 
    | B of string

[<Test>]
let ``test 4``() =
    A 10
    |> should equal (B "abc")

      

... gives me the message:

Expected: Program + MyT + B
  But it was: Program + MyT + A

... which I can workaround by implementing ToString for MyT like this:

override this.ToString() = match this with
    | A i -> sprintf "A(%d)" i
    | B s -> sprintf "B(%s)" s

      

... which will lead to a nice post:

Expected: B (abc)
  But it was: A (10)

... but I would like fsunit to just display the MyT values ​​in a way (sprintf "% A").

Anyway, it is NOT OK to run these workarounds.

How do I get helpful messages for F # lists without using arrays?

How do I get useful messages for ADT?

Is there a good solution for the above problems or should I just give up FsUnit?

Do you have a better recommendation for a unit testing library for F # that doesn't have these issues?

+3


source to share


1 answer


Several contenders:

Expecto

[<Tests>]
let tests =
  testList "test group" [
    testCase "strings" <| fun _ ->
        let subject = "Hello World"
        Expect.equal subject "Hello world"
                    "The strings should be equal"

    testCase "lists" <| fun _ ->
        let expected = [1; 2]   
        Expect.equal expected [1; 3]
                    "The lists should be equal"

    testCase "DUs" <| fun _ ->
        let expected = A 10   
        Expect.equal expected (B "abc")
    ]

      

Output

[19:29:46 INF] EXPECTO? Running tests...
[19:29:46 ERR] test group/strings failed in 00:00:00. 
The strings should be equal.
          Expected string to equal:
          "Hello world"
                 ↑
          The string differs at index 6.
          "Hello World"
                 ↑
          String does not match at position 6. Expected char: 'w', but got 'W'.

[19:29:46 ERR] test group/lists failed in 00:00:00. 
The lists should be equal. Actual value was [1; 2] but had expected it to be [1; 3].

[19:29:46 ERR] test group/DUs failed in 00:00:00. 
The DUs should be equal. Actual value was A 10 but had expected it to be B "abc".

[19:29:46 INF] EXPECTO! 3 tests run in 00:00:00.0028417 – 0 passed, 0 ignored, 3 failed, 0 errored. ( ΰ²° ΔΉΜ― ರೃ )
val it : int = 1

      

Unquote

[<Test>]
let ``The strings should be equal`` () =
    let subject = "Hello World"
    subject =! "Hello world"

      

Result Message:   
"Hello World" = "Hello world"
false

      



[<Test>]
let ``The lists should be equal`` () =
    let expected = [1; 2]
    expected =! [1; 3]

      

Result Message:   
[1; 2] = [1; 3]
false

      

[<Test>]
let ``The DUs should be equal`` () =
    let expected = A 10
    expected =! (B "abc")

      

Result Message:   
A 10 = B "abc"
false

      

The advantage of Unquote lies in its Quotations

allowing step-by-step error reporting.

[<Test>]
let ``The arrays should be equal`` () =
    let expected = [|0 ; 2 ; 3 ; 4|]
    test <@ (Array.map ((+) 1) [|0 .. 3|]) = expected @>

      

Result Message:   
Array.map ((+) 1) [|0..3|] = [|0; 2; 3; 4|]
Array.map ((+) 1) [|0; 1; 2; 3|] = [|0; 2; 3; 4|]
[|1; 2; 3; 4|] = [|0; 2; 3; 4|]
false

      

+2


source







All Articles