2

I'm trying to set-up a test initialise function that handles some basic database set-up tasks; here's my base class:

[ TestClass]
public class BaseTest
{
    private SqlConnection sqlConnection;
    protected SqlTransaction sqlTransaction;

    [TestInitialize ()]
    protected void InitialiseConnection()
    {  
        // Set-up sqlTransaction

    }

    [ TestCleanup ()]
    protected void RollbackConnection()
    {
        // Cleanup
    }

Then I'm using it like this:

[ TestClass]
public class MyTest : BaseTest
{

    [ TestMethod ]
    public void MyFirstTest()
    {
         /// Access DB here crashes because sqltransaction is null

A breakpoint on the base class reveals that it isn't calling the InitialiseConnection method. Have I missed something?

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • I'm not positive, but have you tried making `InitialiseConnection` and `RollbackConnection` `public` instead of `protected`? Just a shot in the dark really (the examples I've seen and used had them public). EDIT: Also, if your break point isn't being hit, are the methods actually empty and are you running in release mode? – Chris Sinclair Nov 29 '13 at 10:52
  • Yup.. it should be public .. you can have a virtual method though which you would like to use when some additional initialization is required. – AsitK Nov 29 '13 at 10:57

1 Answers1

5

Make the method marked with TestInitialize public instead of protected. I remember having similar problems when the signature didn't exactly match the one in the MSDN sample.

chwarr
  • 6,777
  • 1
  • 30
  • 57