6

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?

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • 1
    I think you should consider your test data as readonly. Don't alter the file. Instead, generate useful output from the test including useful information from the row that failed. – Pablo Romeo Jun 25 '13 at 04:39
  • @PabloRomeo thanks for you consideration and that is how I am doing it right now: I output all column headers with row information to the Console and I use the Description attribute to enter the testcase info in Gherkin language so the testcase is written full out so it is retestable even by a non technical person. allthoug it would be of much convenience to output results to a custom column in the Excel file and use conditional formatting to display which data failed in the test, I encountered more difficulties with this approach like check out policies and file lockings. – AutomatedChaos Jun 25 '13 at 09:20
  • Why not just view the failed or passed status from the TestResults.trx using VS? Data-driven tests expand to multiple entries (one for each row in the datasource) if I'm not mistaken. Also, in the past I've used some XSLT transformations publicly available to convert the TRX result to a human-readable html file, for example. – Pablo Romeo Jun 25 '13 at 15:32

1 Answers1

0

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.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72