【问题标题】:Could not assign the dynamic value in testcasesource无法在 testcasesource 中分配动态值
【发布时间】:2025-12-23 12:05:12
【问题描述】:

我使用 testcasesource 开发了单个测试用例。已经从excel读取数据并存储在数组中。现在我想动态地为 testcasesource 分配数组值。我该怎么做?这是我的代码。

[TestFixture]
class testcases 
{
    static String[] inputdata= readdata("Inputdata.xls", "DATA", "TestCase1");
    static object[] exceldata = { new object[] {inputdata} };

    [SetUp]
    public void Setup()
    {
        //setup code here
    }

    [Test]
    [TestCaseSource("exceldata")]
    public void Sample(String Username,String password,String FirstName)
    {
               // test code here
    }   

    [TearDown]
    public void TearDown()
    {
        tstlogic.driverquit();
    }
}

我在 inputdata 数组中有 3 个输入值,我需要为 testcasesource 分配这些值。有人可以帮忙吗?

【问题讨论】:

  • String Username,String password,String FirstName 应该来自哪里?

标签: c# nunit testcasesource


【解决方案1】:

请参阅以下行。它定义了错误的类型。不幸的是,当使用object[] 时很难看到这一点。

static object[] exceldata = { new object[] {inputdata} };
// type is object[] of object[] of string[]

应该是:

static object[] exceldata = { inputdata };
// type is object[] of string[]

【讨论】: