NUnit TestCaseSource

元气小坏坏 提交于 2019-11-30 21:37:48

问题


I'm having a go with the TestCaseSource attribute. One problem: when the sourceName string is invalid, the test just gets ignored instead of failing. This would be really bad if the source method gets renamed, the sourceName string doesn't get updated, and then you lose the coverage that the test provided. Is there a way to change the behaviour of NUnit, so that the test fails if the sourceName is invalid?


回答1:


This is resolved in NUnit 2.6.2. There is a new constructor for the attribute that takes a Type (which must implement IEnumerable); it "is recommended for use in preference to other forms because it doesn't use a string to specify the data source and is therefore more easily refactored." (From the documentation.)

This does require that your test runner supports the latest NUnit.

A very basic example (see the above documentation link for more details):

public class TestDataProvider : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        return new List<int>{ 2, 4, 6 }.GetEnumerator();
    }
}

[TestFixture]
public class MyTests
{
    [TestCaseSource(typeof(TestDataProvider))]
    public void TestOne(int number)
    {
        Assert.That(number % 2, Is.EqualTo(0));
    }
}



回答2:


I've looked how it work internally in NUnit. They just throws exception if particular source does not exists:

MemberInfo[] members = providerType.GetMember(
 providerName,
 MemberTypes.Field | MemberTypes.Method | MemberTypes.Property,
 BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

if (members.Length == 0)
 throw new Exception(string.Format(
  "Unable to locate {0}.{1}", providerType.FullName, providerName));

Later they catch it and mark particular ParameterSet (class which wraps test case source) as RunState.NotRunnable which will later be handled as Ignored test.

So, there is probably no way to change this behavior without changing NUnit code - which is btw available for download. You can also try to contact with NUnit team or just treat ignored test in the same way as failing ones:)




回答3:


How about using nameof feature introduced in C#6

public static class TestCasesData 
{ 
  public static string[] TestStringsData() 
  {
       return new string[] {"TEST1", "TEST2"};
  }
}

[TestFixture]
public class MyTest
{
     [Test]
     [TestCaseSource(typeof(TestCasesData), nameof(TestCasesData.TestStringsData))]
     public void TestCase1(...)
     {
     }
}


来源:https://stackoverflow.com/questions/11722888/nunit-testcasesource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!