我尝试使用Nunit进行数据驱动测试。
// I'm actually using it like this:
public class Test
{
[Test, TestCaseSource(typeof(Test), "TestCaseDataList")]
public void Test_Sum(string paramA, string paramB, string result)
{
int pa = Convert.ToInt32(paramA);
int pb = Convert.ToInt32(paramB);
Soma soma = new Soma();
Assert.AreEqual(result, soma.Sum(pa, pb).ToString());
}
}
public IEnumerable TestCaseDataList
{
get
{
List<TestCaseData> testCaseDataList = new TestDataReader().ReadExcelData(@"C:\Data\TestData.xls", @"Plan1");
foreach (TestCaseData testCaseData in testCaseDataList)
{
yield return testCaseData;
}
}
}
// But i want to use like this:
public class Test
{
[Test, TesteCaseAttribute(@"C:\Data\TestData.xls", @"Plan1")]
public void Test_Sum(string paramA, string paramB, string result)
{
int pa = Convert.ToInt32(paramA);
int pb = Convert.ToInt32(paramB);
Soma soma = new Soma();
Assert.AreEqual(result, soma.Sum(pa, pb).ToString());
}
}
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class TesteCaseAttribute : System.Attribute
{
private string plan;
private string ab;
public TesteCaseAttribute(String plan, String ab)
{
this.plan = plan;
this.ab = ab;
}
}
public IEnumerable TestCaseDataList
{
get
{
List<TestCaseData> testCaseDataList = new TestDataReader().ReadExcelData(this.plan, this.ab);
foreach (TestCaseData testCaseData in testCaseDataList)
{
yield return testCaseData;
}
}
}
如何让TestCaseAttribute
构造函数接收TestCaseDataList
的返回值?