Unit testing 在类属性中访问NUnit测试名称
我正在尝试使用Nunit的数据驱动测试Unit testing 在类属性中访问NUnit测试名称,unit-testing,nunit,Unit Testing,Nunit,我正在尝试使用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(para
// 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;
}
}
}
//我实际上是这样使用它的:
公开课考试
{
[测试,TestCaseSource(类型(测试),“TestCaseDataList”)]
public void Test_Sum(字符串参数、字符串参数、字符串结果)
{
int pa=转换为32(参数);
int pb=转换为32(参数b);
Soma Soma=新的Soma();
aresequal(result,soma.Sum(pa,pb.ToString());
}
}
公共IEnumerable TestCaseDataList
{
得到
{
List testCaseDataList=new TestDataReader().ReadExcelData(@“C:\Data\TestData.xls”,@“Plan1”);
foreach(testCaseDataList中的TestCaseData TestCaseData)
{
收益率测试用例数据;
}
}
}
//但我想这样使用:
公开课考试
{
[测试,TesteCaseAttribute(@“C:\Data\TestData.xls”,@“Plan1”)]
public void Test_Sum(字符串参数、字符串参数、字符串结果)
{
int pa=转换为32(参数);
int pb=转换为32(参数b);
Soma Soma=新的Soma();
aresequal(result,soma.Sum(pa,pb.ToString());
}
}
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method,AllowMultiple=true,Inherited=false)]
公共类TesteCaseAttribute:System.Attribute
{
私有字符串计划;
私有字符串ab;
公共测试用例属性(字符串计划、字符串ab)
{
这个。计划=计划;
this.ab=ab;
}
}
公共IEnumerable TestCaseDataList
{
得到
{
List testCaseDataList=newtestdatareader().ReadExcelData(this.plan,this.ab);
foreach(testCaseDataList中的TestCaseData TestCaseData)
{
收益率测试用例数据;
}
}
}
如何让
TestCaseAttribute
构造函数接收TestCaseDataList
的返回?如果这是您的意思,那么使用TestCaseAttribute是不可能的。如果“TesteCaseAttribute”打算成为您自己的一个新属性,那么您必须以这样一种方式编写该属性:它从excel文件中创建测试用例。描述如何做到这一点更像是一篇杂志文章,而不是简单的回答。:-)你好TesteCaseAttribute是我的新类属性xD。我已经创建了案例,问题是如何在TesteCaseAttribute的构造函数中传递它。目前,我只返回反产量列表,但无法在构造函数上执行此操作:/