Not all tests are run in parameterised NUnit TestFixture containing arrays
Asked Answered
S

4

7

I have a test class as follows:

[TestFixture("someurl1", new[] { "param1", "param2" }, 15)]
[TestFixture("someurl2", new[] { "param3" }, 15)]
public class my_test
{
    public my_test(string url, string[] fields, int someVal)
    {
        // test setup
    }
}

When running this test in ReSharper 6.1 and NUnit 2.5.10, the test is not run twice, as expected, it only runs once. In the test results I see listed

my_test("someurl1", System.String[], 15)

This makes me think that the two fixtures are being treated as the same, and that NUnit isn't differentiating between the string arrays in the two tests.

As a workaround I have added a dummy parameter in the constructor. If I set this to a different value for each fixture, then all the tests run.

Is it not possible to have TestFixtures with arrays containing different values? I've just upgraded from ReSharper 5 so I'm wondering if that is related. I have read about some issues with parameterised tests in 6.x.

Schwarzwald answered 5/4, 2012 at 13:39 Comment(4)
Does it work in the NUnit runner?Kleeman
Also, thanks for pointing out that you can have parameterised test fixtures, as I didn't know this was possible.Kleeman
Just tried in the NUnit runner and that runs all the tests successfully. I think it could be related to the ReSharper upgrade.Schwarzwald
I've had problems before with resharper's runner. I just stick to the NUnit one now.Kleeman
T
4
[TestFixture("someurl1", "param1|param2", 15)]
[TestFixture("someurl2", "param3", 15)]
public class my_test
{
    private string[] _fields;

    public my_test(string url, string fieldList, int someVal)
    {
        _fields = fieldList.Split('|');
        // test setup
    }

    [Test]
    public void listFields()
    {
        foreach (var field in _fields)
        {
            Console.WriteLine(field);
        }
    }
}
Tureen answered 20/12, 2012 at 13:46 Comment(0)
G
0

I think this is related to newing-up an array in the TestFixture constructor, I read somewhere that you can't do that.

Greenwald answered 10/4, 2012 at 9:16 Comment(0)
F
0

Have you tried creating individual tests under it as a generic TestFixture??? Something like

[TestFixture]
public class my_test
{
    private bool my_test(string url, string[] fields, int someVal)
    {
        // test setup
        return DidTestCompleteOk;
    }

    [Test]
    public void TestURL1()
    {
        Assert.IsTrue( my_test("someurl1", new[] { "param1", "param2" }, 15));
    }

    [Test]
    public void TestURL2()
    {
        Assert.IsTrue( my_test("someurl2", new[] { "param3" }, 15) );
    }
}
Frog answered 22/10, 2012 at 11:35 Comment(0)
R
0

It turns out this is the absolute error message if ANYTHING goes wrong in a parameterized test fixture's constructor. You don't get the actual exception information returned like when stuff fails in other code.

So you probably should move your setup code to a [SetUp] or [TestFixtureSetUp] or reallly be sure your constructor is executing without error. But really you should be doing the first suggestion, and only save the test fixture parameters in the constructor and do something with them in another method.

Rhinology answered 30/5, 2013 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.