0

我正在尝试NUnit在阅读CSV fileie后将参数传递给测试

[Test, TestCaseSource(typeof(RegistrationData), "GetTestData")]
public void RegisterUserTest(RegistrationData registrationData)
    {

        RegisterNewUser registration = new RegisterNewUser(this.driver);
        this.driver.Navigate().GoToUrl(baseURL + "/mercuryregister.php");

        registration.registerNewUser(registrationData);
    }

但我得到了错误:

System.InvalidCastException:无法将“RegisterUser.RegistrationData”类型的对象转换为“System.Collections.IEnumerable”.RegisterUser.UserRegistrationTest.RegisterUserTest

 private RegistrationData GetTestData()
    {
        DataTable dt = DataTable.New.ReadCsv(@"C:\datafolder\regdata.csv");
        RegistrationData registrationData = new RegistrationData();

        foreach (Row row in dt.Rows)
        {

            registrationData.setfirstName(row["FirstName"]);
            registrationData.setfirstName(row["LastName"]);
            registrationData.setPhone(row["Phone"]);
            registrationData.setUserName(row["UserName"]);
            registrationData.setAddress1(row["Add1"]);
            registrationData.setAddress2(row["Add2"]);
            registrationData.setCity(row["City"]);
            registrationData.setState(row["State"]);
            registrationData.setPostalcode(row["Postalcode"]);
            registrationData.setCountry(row["Country"]);
            registrationData.setEmail(row["Email"]);
            registrationData.setPassword(row["Password"]);
            registrationData.setConfimPassword(row["Cpassword"]);

        }
        // return new RegistrationData[][] { { registrationData } };
        return registrationData; 
    }
4

1 回答 1

3

ModelTestCaseSource 示例:

public class ModelTestCaseSource
{
    public IEnumerable<TestCaseData> GetTestCases()
    {
        DataTable dt = DataTable.New.ReadCsv(@"C:\datafolder\regdata.csv");

        foreach (Row row in dt.Rows)
        {
            var registrationData = new RegistrationData();

            registrationData.setfirstName(row["FirstName"]);
            registrationData.setfirstName(row["LastName"]);
            registrationData.setPhone(row["Phone"]);
            registrationData.setUserName(row["UserName"]);
            registrationData.setAddress1(row["Add1"]);
            registrationData.setAddress2(row["Add2"]);
            registrationData.setCity(row["City"]);
            registrationData.setState(row["State"]);
            registrationData.setPostalcode(row["Postalcode"]);
            registrationData.setCountry(row["Country"]);
            registrationData.setEmail(row["Email"]);
            registrationData.setPassword(row["Password"]);
            registrationData.setConfimPassword(row["Cpassword"]);

            yield return new TestCaseData(new object[] { registrationData });
        }
    }
}

用法:

[Test, TestCaseSource(typeof(ModelTestCaseSource), "GetTestCases")]
public void RegisterUserTest(RegistrationData registrationData)
{

    RegisterNewUser registration = new RegisterNewUser(this.driver);
    this.driver.Navigate().GoToUrl(baseURL + "/mercuryregister.php");

    registration.registerNewUser(registrationData);
}
于 2013-11-11T14:41:58.353 回答