Paul Michaels
Paul Michaels

Reputation: 16695

Set-up MSUnit tests using inheritance

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?

Upvotes: 2

Views: 357

Answers (1)

chwarr
chwarr

Reputation: 7202

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.

Upvotes: 5

Related Questions