用Excel工作表作为数据源,将测试结果写回TestContext
本文关键字:测试结果 写回 TestContext 数据源 Excel 工作 | 更新日期: 2023-09-27 18:18:39
我们有很多测试,其中测试数据存储在Excel中。我创建了testmethods,其中Excel表作为DataSource
连接到TestContext
。
为了方便起见,我想用测试结果更新Excel表格,以便很容易看到数据(或系统)的错误。
我尝试过的事情:
直接写入TestContext.DataRow
:
TestContext.DataRow.BeginEdit();
TestContext.DataRow["Result"] = "change";
TestContext.DataRow.EndEdit();
TestContext.DataRow.AcceptChanges();
结果:通过,但在我的Excel文件中没有更新行。
通过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.
并通过更改TestContext
下的DataRow
来更新它:
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 });
结果:也通过,但在我的Excel文件中也没有更新行。
以前有人成功地做过这件事吗?或者有人能告诉我哪里是错的吗?
MSTest不支持对数据驱动测试的数据行进行回写,而且它的设计也不是这样的。
我的建议是使用Trace.Write()
等方法将结果打印到测试的每个单独迭代的结果中,这就是我在测试失败的情况下为调试目的所做的。