rychlmoj
rychlmoj

Reputation: 455

F# unable to create NUnit tests from TestFixtureSource and class with multiple parameter constructor

I am new in F# and I wonder whether is there a possibility (unsing NUnit) to construct a test class multiple parameters in its constructor with some similar construction - following end up with Message: OneTimeSetUp: No suitable constructor was found

// if data with one parameter, no problem to run the tests
// the data not only the constants at the compile time, so need to work TestFixtureSoource attribute.
type SimpleFixtureArgs =
    static member Source = [| (String.Empty, String.Empty); ("hello", "hello") |]

[<TestFixtureSource(typeof<SimpleFixtureArgs>, "Source")>]
type ``simple tests class``(text, text2) =
    [<Test>]
    member this.``simple test``() =
        let expexted = text
        let actual = text2
        Assert.AreEqual(expexted, actual)

Since removing the one parameter (e.g. the text2) and having appropriate one-parameter TestFixtureSource it started to work...

So the question is how to write the NUnit test to work with TestFixtureSource with multiple parameters?

TIA, Mojmir

Upvotes: 2

Views: 147

Answers (2)

rychlmoj
rychlmoj

Reputation: 455

Later, I needed to deal with different types of parameters. There is slightly modified code in F# from the previous answer, which worked for me.

    type SimpleFixtureArgs2 =
    static member Source : seq<obj []> =
        seq {
            yield [| String.Empty; String.Empty; 1; 1 |]
            yield [| "hello"; "hello"; 2; 2 |]
        }

[<TestFixtureSource(typeof<SimpleFixtureArgs2>, "Source")>]
type ``simple tests2 class``(text1, text2, num1, num2) =
    [<Test>]
    member this.``simple strings and integers test``() =
        let expextedText = text1
        let actualText = text2
        Assert.AreEqual(expextedText, actualText)

        Assert.AreEqual(num1, num2)

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Individual items of text fixture source should be object arrays or derive from the TestFixtureParameters class (NUnit documentation). But tuple is not an object array - it's a single object. So change source property to return IEnumerbale (or array) of arrays:

type SimpleFixtureArgs =
    static member Source = seq {
        [| String.Empty; String.Empty|]
        [| "hello"; "hello"|]
    }

Upvotes: 1

Related Questions