2

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SWilliams
  • 691
  • 7
  • 28
  • If you are using complex objects in your TestCaseData always make sure that they are properly constructed. Nunit won't yell at you, it will just ignore failed test cases. – Kamil Stadryniak Feb 28 '19 at 08:23

1 Answers1

1

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.

forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • Apologies, I have made some typos in the question. You are right, it should be `public class TestClass` and `public void Create(string Id)`. Thanks for your answer but I'm afraid it does not resolve my issue. – SWilliams Jun 16 '15 at 14:18
  • @SWilliams You should edit your question to fix the typos, so that they don't confuse the issue for other people that haven't read your comment. The test you see should be named `createTest_id123` if it isn't then there is a good chance that the problem is that your test runner doesn't support the testcasesource attribute (the code with a couple of fixes works for me). Have you tried it through the Nunit gui? What test runner are you using? – forsvarir Jun 16 '15 at 14:30
  • I don't think I have privilege to edit my question unfortunately. Thank you very much, I have tried it through the NUnit UI and it works as expected! You are right, it is a problem for me as I am running it through VS2010. Is there anything I can install that will support TestCaseSource attribute running through my IDE? – SWilliams Jun 16 '15 at 14:38
  • @SWilliams You can always edit your own question. If you tried before and it failed, it's possible that you did it while I was adding the unit-testing tag. I use the NUnit Test Adapter, installed via Nuget package manager, but I'm using VS2012. I can't remember if it worked with VS2010 or not. I believe addins like Resharper should support it, but obviously there's a price associated with that... – forsvarir Jun 16 '15 at 14:43
  • My mistake, I have edited the question now I have switched to your setup on VS2012 and it is working. I did have an old Resharper version on VS2010, I guess this was the problem but I could not find a suitable extension to let me run this through VS2010 so upgraded instead. Thanks for your help – SWilliams Jun 16 '15 at 14:58