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

前端 未结 6 764
故里飘歌
故里飘歌 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:05

    As described in this question, you can only use literals with InlineData. Lists are not literals.

    However, xUnit provides with ClassData which seems to do what you need.

    This question discusses the same problem for C#.

    In order to use ClassData with the tests, just make a data class implementing seq:

    type MyArrays () =    
        let values : seq  =
            seq {
                yield [|3; 4|]    // 1st test case
                yield [|32; 42|]  // 2nd test case, etc.
            }
        interface seq with
            member this.GetEnumerator () = values.GetEnumerator()
            member this.GetEnumerator () =
                values.GetEnumerator() :> System.Collections.IEnumerator
    
    module Theories = 
        []
        [)>]
        let ``given an array it should be able to pass it to the test`` (a : int, b : int) : unit = 
            Assert.NotEqual(a, b)
    

    Albeit this requires some manual coding, you may re-use the data class, which appears to be useful in real-life projects, where we often run different tests against the same data.

提交回复
热议问题