views:

261

answers:

1

I am setting up some MSTest based unit tests. To make my life easier I want to use a base class that handles the generic setup and taredown all of my tests require. My base class looks like this:

[TestClass]
public class DBTestBase {
    public TestContext TestContext { get; set; }

    [ClassInitialize()]
    public static void MyClassInitialize(TestContext testContext) {
        var config = new XmlConfigurationSource("ARconfig_test.xml");
        ActiveRecordStarter.Initialize(Assembly.Load("LocalModels"), config);
    }

    [TestInitialize()]
    public void MyTestInitialize() {
        ActiveRecordStarter.CreateSchema();
        Before_each_test();
    }

    protected virtual void Before_each_test() { }

    [TestCleanup()]
    public void MyTestCleanup() {
        After_each_test();
    }

    protected virtual void After_each_test() { }
}

My actual test class looks like this:

[TestClass]
public class question_tests : DBTestBase {

    private void CreateInitialData() {
        var question = new Question()
                           {
                               Name = "Test Question",
                               Description = "This is a simple test question"
                           };
        question.Create();
    }

    protected override void Before_each_test() {
        base.Before_each_test();
        CreateInitialData();
    }

    [TestMethod]
    public void test_fetching() {
        var q = Question.FindAll();
        Assert.AreEqual("Test Question", q[0].Name, "Incorrect name.");
    }
}

The TestInitialize function works as expected. But the ClassInitialize function never runs. It does run if I add the following to my child class:

    [ClassInitialize()]
    public static void t(TestContext testContext) {
        MyClassInitialize(testContext);
    }

Is it possible to get my base class initialize function to run without referencing it in my child class?

+1  A: 

Confirm this was a problem for me too. I used a constructor on the base and a destructor for the cleanup

Jean-Pierre Fouche