We have a lot of tests where the test data is stored in Excel. I created testmethods where the Excel sheets are connected as a DataSource
to the TestContext
.
For convenience reasons, I want to update the Excel sheets with the result of the tests so it is easy to see where the data (or the system) is wrong.
Things I tried:
Writing it directly to the TestContext.DataRow
:
TestContext.DataRow.BeginEdit();
TestContext.DataRow["Result"] = "change";
TestContext.DataRow.EndEdit();
TestContext.DataRow.AcceptChanges();
Result: passes, but no rows are updated in my Excel file.
Updating it through the DataConnection
:
string currentRow = TestContext.DataRow["RowId"].ToString();
System.Data.Common.DbCommand cmd = TestContext.DataConnection.CreateCommand();
cmd.CommandText = String.Format("UPDATE {0} SET {1} = pass WHERE {2} = {3}", sheetName, columnName, "RowId", currentRow);
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteReader();
Result: System.Data.OleDb.OleDbException: Syntax error in UPDATE statement.
And updating it by changing the DataRow
under the TestContext
:
string currentRow = TestContext.DataRow["RowId"].ToString();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.UpdateCommand = new OleDbCommand(String.Format("UPDATE {0} SET {1} = pass WHERE {2} = {3}", sheetName, columnName, "RowId", currentRow));
adapter.UpdateCommand.Connection = (OleDbConnection)TestContext.DataConnection;
adapter.Update(new System.Data.DataRow[] { TestContext.DataRow });
Result: also passes, but no rows are updated in my Excel file too.
Did somebody did this before succesfully? Or does someone have an hint where I could be wrong?
MSTest doesn't support writing back to the data rows for data driven tests, and it's not designed to do this.
My suggestion is to use
Trace.Write()
etc. methods to print the results into the results for each individual iteration of the test, which is what I do for debugging purposes in the case of failed tests.