为什么在使用 TestCaseSource 时 nUnit 测试会被忽略?
Why are nUnit tests just getting ignored when using TestCaseSource?
我很难让 nUnit TestCaseSource 属性在 nUnit 2.6.4.14350 中正常工作。
当 运行 单元测试通过 VS2010 时,它只是说测试被忽略,没有关于原因的任何附加信息。 Create
测试在测试结果查看器中显示为灰色。
在我的例子中,被测试的 class 是 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
}
}
线索:
- CreateBondTestCases getter 受到打击。 (在调用
[TestFixtureSetUp]
之前)。
[TestCaseSource(typeof(TestClass), ...)]
没有任何改变。
public TestClass()
没有任何改变。
public IEnumberable<TestCaseSource> CreateTestCases
或 public IEnumberable CreateTestCases
没有任何改变。
[TestCaseSource(typeof(TestClass), "asdfasdf")]
结果失败:System.Reflection.TargetParameterCountException:参数计数不匹配。
我看到了this question but still can't get it to work. I have also tried to get it to work as in this answer,结果一样。我想我一定是在做一些非常愚蠢的事情,但我太生气了,看不出它是什么。请问有没有人能赐教一下?
您的问题很可能是由于您的测试 运行 不支持 TestCaseSource
属性引起的,尤其是当它被报告为 Create
时。 NUnit 重命名使用 TestCaseSource 的测试,以将参数名称组合到测试名称中。对于您的测试,应报告为 createTest_id123
(测试名称 + 参数值)。
使用 NUnit GUI 检查您的程序集,以查看当 运行 从那里开始时测试是否按预期工作。如果他们这样做,那么这将确认您的 TestRunner 是问题所在。使用 VS2012+,您可以使用通过 Nuget 安装的 NUnit 测试适配器 运行 您的测试。 Resharper 等其他插件(取决于版本)应该支持该属性,但我不确定哪些版本适用于 VS2010。
我很难让 nUnit TestCaseSource 属性在 nUnit 2.6.4.14350 中正常工作。
当 运行 单元测试通过 VS2010 时,它只是说测试被忽略,没有关于原因的任何附加信息。 Create
测试在测试结果查看器中显示为灰色。
在我的例子中,被测试的 class 是 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
}
}
线索:
- CreateBondTestCases getter 受到打击。 (在调用
[TestFixtureSetUp]
之前)。 [TestCaseSource(typeof(TestClass), ...)]
没有任何改变。public TestClass()
没有任何改变。public IEnumberable<TestCaseSource> CreateTestCases
或public IEnumberable CreateTestCases
没有任何改变。[TestCaseSource(typeof(TestClass), "asdfasdf")]
结果失败:System.Reflection.TargetParameterCountException:参数计数不匹配。
我看到了this question but still can't get it to work. I have also tried to get it to work as in this answer,结果一样。我想我一定是在做一些非常愚蠢的事情,但我太生气了,看不出它是什么。请问有没有人能赐教一下?
您的问题很可能是由于您的测试 运行 不支持 TestCaseSource
属性引起的,尤其是当它被报告为 Create
时。 NUnit 重命名使用 TestCaseSource 的测试,以将参数名称组合到测试名称中。对于您的测试,应报告为 createTest_id123
(测试名称 + 参数值)。
使用 NUnit GUI 检查您的程序集,以查看当 运行 从那里开始时测试是否按预期工作。如果他们这样做,那么这将确认您的 TestRunner 是问题所在。使用 VS2012+,您可以使用通过 Nuget 安装的 NUnit 测试适配器 运行 您的测试。 Resharper 等其他插件(取决于版本)应该支持该属性,但我不确定哪些版本适用于 VS2010。