title: C# Unit Testing with TestInitialize/TestCleanup in base class
body: <p>I am testing a module where every test class share the same behavior:</p>
  
  <ul>
  <li>Begin a transaction</li>
  <li>Execute SQL queries</li>
  <li>Rollback transaction </li>
  </ul>
  
  <p>I've decided to use TestInitialize and TestCleanup to execute the Begin and Rollback of transactions respectively.</p>
  
  <p>The strait forward approach would be writing the TestInitialize/TestCleanup in a parent class but that is not going to work with this testing framework.</p>
  
  <p>The <strong>work around</strong> for this was to use <strong>partial classes</strong>. This approach seems to viable in my case because my test classes are mainly stateless. Event not being the ideal solution it at least saved me a couple of copy/paste actions.</p>
  
  <p>Anyone knows a better way to do this?</p>
  
  <p>Here is a sample of the <strong>partial class</strong> solution:</p>
  
  <p>In my case I test each module separately and for this example I will use the <strong>Sales</strong> module:</p>
  
  <p><strong>SalesTest.cs file</strong>:</p>
  
  <pre><code>[TestClass]
  public partial class SalesTest
  {
      [TestInitialize]
      public void Setup()
      {
          //begin transaction
      }
  
      [TestCleanup]
      public void Cleanup()
      {
          //rollback transaction
      }
  }
  </code></pre>
  
  <p><strong>SalesTest.Order file</strong>:</p>
  
  <pre><code>public partial class SalesTest
  {
      [TestMethod]
      public void SaveOrder_OnlyRequiredValuesFilled_SuccessfullySaved()
      {
          //Run some SQL queries 
      }
  }
  </code></pre>