In F# how do you pass a collection to xUnit's InlineData attribute

前端 未结 6 733
故里飘歌
故里飘歌 2021-01-02 00:28

I would like to be about to use a list, array, and/or seq as a parameter to xUnit\'s InlineData.

In C# I can do this:

using Xunit; //2.1.0

namespace C         


        
6条回答
  •  隐瞒了意图╮
    2021-01-02 01:00

    You can use the FSharp.Reflection namespace to good effect here. Consider some hypothetical function isAnswer : (string -> int -> bool) that you want to test with a few examples.

    Here's one way:

    open FSharp.Reflection
    open Xunit
    
    type TestData() =
      static member MyTestData =
        [ ("smallest prime?", 2, true)
          ("how many roads must a man walk down?", 41, false) 
        ] |> Seq.map FSharpValue.GetTupleFields
    
    [)>]
    let myTest (q, a, expected) =
      Assert.Equals(isAnswer q a, expected)
    

    The key thing is the |> Seq.map FSharpValue.GetTupleFields line. It takes the list of tuples (you have to use tuples to allow different arguments types) and transforms it to the IEnumerable that XUnit expects.

提交回复
热议问题