Writing test results back to TestContext using Excel WorkSheet as a data source

We have many tests in which test data is stored in Excel. I created testmethods where Excel worksheets are linked as a DataSource to TestContext .

For reasons of convenience, I want to update Excel worksheets with test results, so it's easy to see where the data (or system) is wrong.

Things I tried:

Direct write to 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.

Update using 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, changing the DataRow under 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.

Has anyone done this before? Or someone tell me where I can be wrong?

+6
source share
1 answer

MSTest does not support writing to data strings for data-driven tests, and is not intended to do so.

My suggestion is to use the Trace.Write() methods, etc. to output the results to the results for each individual test iteration, which I do for debugging in the case of failed tests.

0
source

Source: https://habr.com/ru/post/947476/


All Articles