Unit Testing - dependant tests
I am creating a list of unit tests that are dependant on each other. For example, my first test creates a record in the database and checks that the return value is greater than 0.

The second test then checks the data of the record created in the first test. However, it needs the ID of the record produced in the first test.

Originally I called the second test from within the first test so that I could pass the ID as a parameter, which worked fine except this meant that essentially there was only one test.

I created an ordered list with the ID declared outside the scope but after the first unit test this value returns to 0 so obviously the second unit test fails.

Is there any way to create the tests so that they share the value produced in the first test?

The code is below:

    [TestMethod]
        public void TestNewLandlord_InsertIntoImportFiles_ReturnFileID()
        {
            try
            {
                DataSet ds = EngineBllUtility.InsertIntoImportFiles(connString, @"C:\Documents and Settings\dTrunley\My Documents", "HFISNewLandlordTest.csv",
                "TestNewLandlord()", WindowsIdentity.GetCurrent().Name, "HFIS Landlords", "NA", 30247531, false);

                importFileId = long.Parse(ds.Tables[0].Rows[0]["ImportFileID"].ToString());
                Assert.IsTrue(importFileId > 0);
                SetImportFileId(importFileId);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void TestNewLandlord_InsertIntoImportFiles_CorrectData()
        {
            try
            {
                using (SqlConnection connectionString = new SqlConnection(connString))
                {
                    using (SqlCommand sqlCommand = new SqlCommand(
                        String.Format("SELECT * FROM [mydeposits].[import].[ImportFiles] WHERE [ImportFileID] = {0}", importFileId), connectionString))
                    {
                        connectionString.Open();
                        using (SqlDataReader dr = sqlCommand.ExecuteReader())
                        {
                            if (dr.HasRows)
                            {
                                bool correctData = true;
                                dr.Read();
                                if (!dr["ImportFileStatusID"].ToString().Equals("1"))
                                    correctData = false;
                                if (!dr["HeadOfficeMemberID"].ToString().Equals("247531"))
                                    correctData = false;
                                Assert.IsTrue(correctData);
                                TestCleanup();
                            }
                            else
                                throw new Exception("Import does not exist in database");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
                TestCleanup();
            }
        }
Unit Testing - dependant tests
===

I am creating a list of unit tests that are dependant on each other. For example, my first test creates a record in the database and checks that the return value is greater than 0.

The second test then checks the data of the record created in the first test. However, it needs the ID of the record produced in the first test.

Originally I called the second test from within the first test so that I could pass the ID as a parameter, which worked fine except this meant that essentially there was only one test.

I created an ordered list with the ID declared outside the scope but after the first unit test this value returns to 0 so obviously the second unit test fails.

Is there any way to create the tests so that they share the value produced in the first test?

The code is below:

    [TestMethod]
        public void TestNewLandlord_InsertIntoImportFiles_ReturnFileID()
        {
            try
            {
                DataSet ds = EngineBllUtility.InsertIntoImportFiles(connString, @"C:\Documents and Settings\dTrunley\My Documents", "HFISNewLandlordTest.csv",
                "TestNewLandlord()", WindowsIdentity.GetCurrent().Name, "HFIS Landlords", "NA", 30247531, false);

                importFileId = long.Parse(ds.Tables[0].Rows[0]["ImportFileID"].ToString());
                Assert.IsTrue(importFileId > 0);
                SetImportFileId(importFileId);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void TestNewLandlord_InsertIntoImportFiles_CorrectData()
        {
            try
            {
                using (SqlConnection connectionString = new SqlConnection(connString))
                {
                    using (SqlCommand sqlCommand = new SqlCommand(
                        String.Format("SELECT * FROM [mydeposits].[import].[ImportFiles] WHERE [ImportFileID] = {0}", importFileId), connectionString))
                    {
                        connectionString.Open();
                        using (SqlDataReader dr = sqlCommand.ExecuteReader())
                        {
                            if (dr.HasRows)
                            {
                                bool correctData = true;
                                dr.Read();
                                if (!dr["ImportFileStatusID"].ToString().Equals("1"))
                                    correctData = false;
                                if (!dr["HeadOfficeMemberID"].ToString().Equals("247531"))
                                    correctData = false;
                                Assert.IsTrue(correctData);
                                TestCleanup();
                            }
                            else
                                throw new Exception("Import does not exist in database");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
                TestCleanup();
            }
        }