title: Unit Testing - dependent tests
body: <p>I am creating a list of unit tests that are dependent on each other. For example, my first test creates a record in the database and checks that the return value is greater than 0.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Is there any way to create the tests so that they share the value produced in the first test?</p>
  
  <p>The code is below:</p>
  
  <pre><code>[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 &gt; 0);
      }
      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();
      }
  }
  </code></pre>