【发布时间】:2015-06-16 23:27:06
【问题描述】:
我很难让 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
}
}
线索:
- CreateBondTestCases getter 被击中。 (在调用
[TestFixtureSetUp]之前)。 -
[TestCaseSource(typeof(TestClass), ...)]不会改变任何东西。 -
public TestClass()不会改变任何东西。 -
public IEnumberable<TestCaseSource> CreateTestCases或public IEnumberable CreateTestCases不会改变任何东西。 -
[TestCaseSource(typeof(TestClass), "asdfasdf")]导致失败:System.Reflection.TargetParameterCountException:参数计数不匹配。
我看到了this question,但仍然无法正常工作。我也试图让它像this answer 一样工作,结果相同。我想我一定是在做一些非常愚蠢的事情,但我太生气了,看不出它是什么。有大神能指教一下吗?
【问题讨论】:
-
如果您在 TestCaseData 中使用复杂的对象,请始终确保它们的构造正确。 Nunit 不会对你大喊大叫,它只会忽略失败的测试用例。
标签: c# .net unit-testing nunit