我很难让 nUnit TestCaseSource 属性在 nUnit 2.6.4.14350 中正常工作。
当通过 VS2010 运行单元测试时,它只是说测试被忽略,没有关于原因的任何额外信息。
Create
测试在测试结果查看器中显示为灰色。
在我的例子中,被测试的类是 TestCaseSource 本身。这就是我所拥有的:
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
}
}
线索:
[TestFixtureSetUp]
被调用之前)。[TestCaseSource(typeof(TestClass), ...)]
没有任何改变。public TestClass()
没有任何改变。public IEnumerable<TestCaseSource> CreateTestCases
或 public IEnumerable CreateTestCases
不会改变任何东西。[TestCaseSource(typeof(TestClass), "asdfasdf")]
导致失败:System.Reflection.TargetParameterCountException:参数计数不匹配。我看到了this question但仍然无法让它工作。我也试图让它像this answer一样工作,结果相同。我想我一定是在做一些非常愚蠢的事情,但我太生气了,看不出它是什么。请有人能赐教吗?
您的问题很可能是由您的测试运行程序不支持
TestCaseSource
属性引起的,特别是如果它被报告为Create
。 NUnit 重命名使用 TestCaseSource 的测试,以将参数名称组合到测试名称中。对于您的测试,应报告为createTest_id123
(测试名称+参数值)。
使用 NUnit GUI 检查您的程序集,以查看从那里运行时测试是否按预期工作。如果他们这样做,那么这将确认您的 TestRunner 是问题所在。使用 VS2012+,您可以使用通过 Nuget 安装的 NUnit 测试适配器来运行您的测试。 Resharper 等其他插件(取决于版本)应该支持该属性,但我不确定哪些版本适用于 VS2010。