开发者

Using inheritance in MSTest

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",
      开发者_如何学C                         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?


ClassInitialize method is executed if and only if the concerned "class" contains at least one TestMethod, and at least one TestMethod from the class is selected for execution.


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


[TestClass]
public class question_tests : DBTestBase {
...
    [TestCleanup()]
    public void TestCleanup()
    {
        base.MyTestCleanup();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜