My method to read the excel rows using linqToExcel is below
public static IQueryable<LinqToExcel.Row> linqTesting2() { var excel = new ExcelQueryFactory(Constants.testDataPath4); var testcases = from c in excel.Worksheet("Sheet1") where c["UserName"] == "SonjaB" select c; return testcases; }
To use the row data in my unit test do I need to somehow convert it to a IEnumerable<string[]> before it gets returned to the test. If so any ideas on how to do this would be appreciated. When I ran the test the only error I get is I need to pass the correct amount of parameters.
Thanks
C#.NET ProgrammingProgramming
Last Comment
kaufmed
8/22/2022 - Mon
Karrtik Iyer
So you want all the column values of a excel row to be converted to an array of string?
victoriaharry
ASKER
Yes, so each row of data can be passed into the NUnit test
Karrtik Iyer
Something like below.
var testcases = (from Row r in excel.Worksheet("Sheet1") where r["UserName"] == "SonjaB" select r.ToArray()).AsEnumerable(); IEnumerable<String[]> values = (from cells in testcases select cells.Select(x => x.Value.ToString()).ToArray()).AsEnumerable();
You can combine into a single statement as well, like below:
IEnumerable<string[]> testcases = (from Row r in excel.Worksheet("Sheet1") where r["UserName"] == "SonjaB" select r).AsEnumerable().Select( x => x.ToArray().Select( y => y.Value.ToString()).ToArray());
Based on what I'm seeing in the documentation, you should be creating an anonymous object that has each piece of data expected by your parameters:
public static IEnumerable<object> linqTesting2(){ var excel = new ExcelQueryFactory(Constants.testDataPath4); var testcases = from c in excel.Worksheet("Sheet1") where c["UserName"] == "SonjaB" select new { username = c["UserName"].Value, password = c["Password"].Value }; return testcases;}
It appears that you add the properties to the anonymous object in the order that the test method lists parameters. My guess is that NUnit uses reflection to loop over the object properties, but I left the property names the same as the parameter names (including casing) just in case.
You also don't need AsEnumerable, ToArray, or any other similar method. The result of a LINQ query is already IEnumerable, so just go with that. You will note that I changed the return type of the factory method.
victoriaharry
ASKER
Thanks for the responses guys. When I run the test in Test Explorer using either of the responses above I get the error "Not enough arguments provided, provide at least two arguments. When I build the solution I can see the test explorer is creating the correct amount of tests and I can see in test explorer the data next to the test is from the excel file rows. However when I ran one of the tests I get the error. The test I'm running is below
[Test, TestCaseSource("linqTesting2")] public void CompleteFormTestDataSource4(String username, String password) { LoginPageObject pageLogin = new LoginPageObject(); EAPageObject pageEA = pageLogin.Login(username, password); }