Search code examples
c#.netunit-testingnunit

Why are nUnit tests just getting ignored when using TestCaseSource?


I am having a lot of difficulty getting the nUnit TestCaseSource attribute to work correctly in nUnit 2.6.4.14350.

When running the unit tests through VS2010, it just says the tests are being ignored, without any additional information as to why. The Create test just appears greyed out on the test results viewer.

In my case the class being tested is the TestCaseSource itself. This is what I've got:

public class TestClass
{
    static TestClass()     
    {
        PopulateIds();
    }

    public IEnumerable<object> CreateTestCases
    {
        get 
        {
            return _ids.Select(id => new TestCaseData(id).SetName("createTest_" + id));
        }
    }

    private static string[] _ids;

    private static void PopulateIds()
    {
        _ids = new string[] { "id123", // ... }
    }

    [TestFixtureSetUp]
    public void Init()
    {
        // this completes successfully
    }

    [Test, TestCaseSource("CreateTestCases")]
    public void Create(string Id)
    {
        // breakpoint here is never hit as test is ignored
    }
}

Clues:

  • CreateBondTestCases getter is getting hit. (before [TestFixtureSetUp] is called).
  • [TestCaseSource(typeof(TestClass), ...)] doesn't change anything.
  • public TestClass() doesn't change anything.
  • public IEnumerable<TestCaseSource> CreateTestCases or public IEnumerable CreateTestCases doesn't change anything.
  • [TestCaseSource(typeof(TestClass), "asdfasdf")] results in Failed: System.Reflection.TargetParameterCountException : Parameter count mismatch.

I saw this question but still can't get it to work. I have also tried to get it to work as in this answer, with the same results. I guess I must be doing something pretty stupid here but I am too infuriated to see what it is. Please can somebody enlighten me?


Solution

  • It's likely that your issue is being caused by your test runner not supporting the TestCaseSource attribute, particularly if it is being reported as Create. NUnit renames tests that use TestCaseSource, to combine the argument names into the name of the test. For your test, it should be reported as createTest_id123 (the name of the test + the value of the parameters).

    Check your assembly using the NUnit GUI to see if the tests work as expected when run from there. If they do, then this will confirm that your TestRunner is the issue. With VS2012+, you can use the NUnit Test Adapter, installed via Nuget to run your tests. Other addins like Resharper (depending on the version) should support the attribute, but I'm not sure what versions will work with VS2010.