Chris Wignall
Chris Wignall

Reputation: 145

Not all tests are run in parameterised NUnit TestFixture containing arrays

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.

Upvotes: 7

Views: 2065

Answers (4)

craastad
craastad

Reputation: 6472

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.

Upvotes: 0

mrtgold
mrtgold

Reputation: 61

[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);
        }
    }
}

Upvotes: 4

DRapp
DRapp

Reputation: 48139

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) );
    }
}

Upvotes: 0

KlsLondon
KlsLondon

Reputation: 1280

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

Upvotes: 0

Related Questions