Passing data to Nunit Test using C#

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Passing data to Nunit Test using C#

navin

I am passing the data read from the CSV file to the test as shown below:

[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);
    }

The method I am using to create the data is :

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;
        }

This causes an error shown below :

System.InvalidCastException : Unable to cast object of type 'RegisterUser.RegistrationData' to type 'System.Collections.IEnumerable'. RegisterUser.UserRegistrationTest.RegisterUserTest

Doe's anyone  know how to fix this problem.

Thanks