locked
Adding data to the DB for a Data Driven Unit Test RRS feed

  • Question

  • I have finally got my unit test just about ready but I need to add some data to the DB during the first run.  I am sending data through and getting a return value.  I want to add that return value to the Database so I can then organize all the tests by expected return value.

    Here is what my code looks like currently (some things changed to protect company code):

    [Timeout(1000000), DataSource("System.Data.SqlClient", "Data Source=MyBox\\sqlexpress;Initial Catalog=Test_DB;Integrated Security=True", "Test_Table", DataAccessMethod.Sequential), TestMethod]

    public void Status()

    {

    int p_DataID = (int)TestContext.DataRow[(int)Column.DataID];

    string p_Data1= (string)TestContext.DataRow[(int)Column.Data1];

    string p_Data2 = TestContext.DataRow[(int)Column.Data2].ToString();  //Can be null so needs ToString()

    string p_Data3 = (string)TestContext.DataRow[(int)Column.Data3];

    string p_Data4 = (string)TestContext.DataRow[(int)Column.Data4];

    string p_Data5 = (string)TestContext.DataRow[(int)Column.Data5];

    MyCode target = new MyCode(p_Data1, p_Data2, p_Data3, p_Data4, p_Data5);

    string p_ReturnData = target.VerifyData();

    Assert.IsNotNull(p_ReturnData);

    TestContext.DataRow.BeginEdit();

    TestContext.DataRow[(int)Column.Data6] = p_ReturnData;

    TestContext.DataRow.AcceptChanges();

    Thread.Sleep(500);

    When I run this in debug mode I see the data being changed and the RowState shows Unchanged (I tried with BeginEdit and EndEdit and the row showed Modified before I ran AcceptChanges but then read AcceptChanges runs EndEdit silently).  When I check the DB though the Data6 row is still blank.

    All I want to do is step through each row of data and record to the new column the return value.  It seems easy but for some reason is not working.

    BTW, why does the forum posting always double space my posts?

    Wednesday, August 16, 2006 6:19 PM

Answers

  • Hello
    The problem is that you are only updating the in memory data and not the data in the database. To update a row in the database table you need to use an "UPDATE" SQL command. For example the following code is from a test method that tests an add function. It then stores the return data from the test in the database.

    int val1 = Int32.Parse(testContextInstance.DataRow["val1"].ToString()); ; // TODO: Initialize to an appropriate value

                int val2 = Int32.Parse(testContextInstance.DataRow["val2"].ToString());// TODO: Initialize to an appropriate value

                int expected = Int32.Parse(testContextInstance.DataRow["val1"].ToString()) + Int32.Parse(testContextInstance.DataRow["val2"].ToString());
                int actual;
                actual = target.Add(val1, val2);
                //rowcount is my index field so I know what row to update.
                string myrowcount = testContextInstance.DataRow["rowcount"].ToString();
                //testContextInstance is my ContextInstance for this test.
                System.Data.Common.DbCommand cmd = testContextInstance.DataConnection.CreateCommand();
                cmd.CommandText = "UPDATE Table1 SET test = " + actual.ToString() + " WHERE rowcount = " + myrowcount; ;
                cmd.ExecuteReader();

     

    Hope this helps.

    Thanks
    Brian [MSFT]
    Microsoft Developer Support
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, August 17, 2006 9:18 PM

All replies

  • Hello
    The problem is that you are only updating the in memory data and not the data in the database. To update a row in the database table you need to use an "UPDATE" SQL command. For example the following code is from a test method that tests an add function. It then stores the return data from the test in the database.

    int val1 = Int32.Parse(testContextInstance.DataRow["val1"].ToString()); ; // TODO: Initialize to an appropriate value

                int val2 = Int32.Parse(testContextInstance.DataRow["val2"].ToString());// TODO: Initialize to an appropriate value

                int expected = Int32.Parse(testContextInstance.DataRow["val1"].ToString()) + Int32.Parse(testContextInstance.DataRow["val2"].ToString());
                int actual;
                actual = target.Add(val1, val2);
                //rowcount is my index field so I know what row to update.
                string myrowcount = testContextInstance.DataRow["rowcount"].ToString();
                //testContextInstance is my ContextInstance for this test.
                System.Data.Common.DbCommand cmd = testContextInstance.DataConnection.CreateCommand();
                cmd.CommandText = "UPDATE Table1 SET test = " + actual.ToString() + " WHERE rowcount = " + myrowcount; ;
                cmd.ExecuteReader();

     

    Hope this helps.

    Thanks
    Brian [MSFT]
    Microsoft Developer Support
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, August 17, 2006 9:18 PM
  • That looks like what I want to do.  I built a little workaround for the time being but this will help next time I need to do something like this.  I have almost all the coding done and did a trial run on my load test last night and it went great.  We only had .023% of the tests come back over the max response time we want to use.  I want to fix the logging for which sets of data come back with what timing but that is the easy part.

    Thanks for all the help in this forum.  It is encouraging to know that there is a forum that people actually provide you with correct answers quickly.

    John

    Thursday, August 17, 2006 9:28 PM
  • Adding the following code to the end of your method is one way to do it:


    SqlDataAdapter adapter = new SqlDataAdapter();

    adapter.UpdateCommand = new SqlCommand("Update Test_Table set Data6 = " + p_ReturnData.ToString() + " Where DataID = " + p_DataID);

    adapter.UpdateCommand.Connection = (SqlConnection)TestContext.DataConnection;

    adapter.Update(new DataRow[] {TestContext.DataRow});


     Will of course have to update the table/field names to match what you have.

    Thursday, August 17, 2006 9:38 PM
    Moderator
  • What happens if the source is a file...... the test context bombs out indicating that the file is already in use when utilizing a streamwriter to the file...... Any thoughts on this aspect??
    Tuesday, December 11, 2012 11:30 PM