locked
Unit test passes when Debug but fails when Run - VS2010 RRS feed

  • Question

  • Hi,

    I'm using VS2010 and writing a unit test to execute a sql command.

    Here is my code:

     private DataRowCollection ReadAOTea_Value(int ddfID, CollectTime collectTime)
    {
              string sql = string.Format("SELECT EntryTypeId, Value FROM tbl_Data

                                                          WHERE ddfID = {0} and DateImported > '{1}'", ddfID, collectTime.from);
              return Common.ExecuteSQL(connString, sql).Rows;
    }

    public static class Common
    {
        public static DataTable ExecuteSQL(string conn, string sql)
        {
            return DatabaseUtils.GetData(conn, sql);
        }
    }

    public static class DatabaseUtils
    {
        public static DataTable GetData(string connString, string sql)
        {
                SqlDataAdapter adapter = null;
                SqlConnection conn = null;
                try
                {
                    conn = new SqlConnection(connString);
                    SqlCommand command = conn.CreateCommand();
                    command.CommandText = sql;
                    command.CommandTimeout = CommandTimeout; //seconds
                    command.CommandType = CommandType.Text;
                    adapter = new SqlDataAdapter(command);
                    conn.Open();
                    var dtReturn = new DataTable();
                    adapter.Fill(dtReturn);
                    return dtReturn;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
                finally
                {
                    if (adapter != null) adapter.Dispose();
                    if (conn != null)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
        }
    }

    Can anyone explain why I receive different result in DEBUG and RUN mode? When I debug, I always get 136 rows returned from the first function ReadAOTea_Value while RUN always return 0.

    Thank you very much for your help.

    Monday, June 24, 2013 3:58 AM

All replies

  • By the way, I logged the sql command and ran it on sql server and it always give the right result which is 136 rows.
    Monday, June 24, 2013 4:06 AM
  • Hi,

    Click Build->Configuration Manager in your VS IDE, make sure that all projects are in debug mode(not the Release), and select “Build” option for your test project, and then clean and rebuild your project, check it again.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 25, 2013 8:27 AM
  • Hi,

    Thank you. I checked my VS IDE and all projects are in debug mode. My test project is selected "Build" too.

    Tuesday, June 25, 2013 8:32 AM
  • Hi,

    Could you share us the detailed error message in the Test Results window after you run your test? If you run it in other machine, does it have the same issue?

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 26, 2013 3:07 AM
  • Hi,

    I only compare the output, so the Test Result says Assert.AreEqual failed.

    I logged the sql and then run on SQL server, it returns data while C# does not.

    I have no chance to run on another machine.

    Thanks.

    Wednesday, June 26, 2013 3:17 AM
  • Hi,

    If you double click the test result in the Test result windows, could you find more error message?

    If not, I’m afraid that it is hard for me to repro this issue. Please run your test in command line, check it again.

    http://msdn.microsoft.com/en-us/library/ms182489(v=vs.100).aspx

    If still no help, would you mind sharing me a simple sample? I will try to run it in my side. You could send it to jackzhai at Hotmail dot com.

    If there's any concern, please feel free to let me know.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 26, 2013 8:55 AM
  • Was there ever a resolution on this problem?

    I am having the exact same problem, but with unrelated C++ AMP code.

    Everything passes when run in Debug, and I put a copy of my tests into my app and ran it both debug and Release and it passes just fine. It only fails in Test Explorer Run Tests mode.


    Friday, July 19, 2013 10:02 PM