Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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",
                               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?

like image 227
oillio Avatar asked Apr 28 '10 19:04

oillio


People also ask

What is ClassInitialize attribute in MSTest?

The method decorated by [ClassInitialize] is called once before running the tests of the class. In some cases, you can write the code in the constructor of the class. The method decorated by [ClassCleanup] is called after all tests from all classes are executed.

Does MSTest work with .NET core?

In this article, I will explain about the unit test in asp.net core using MSTest. There are three different test frameworks which are supported by the unit test with asp.net core: MSTest, xUnit, and NUnit, which allow us to test our code in a consistent way.

What is TestContext MSTest?

The method [ClassInitialize] public static void SetupTests(TestContext testContext) { } is called before the property set TestContext is set. So if you need the context in SetupTests then the parameter is usefull.

Does TestInitialize run for each test?

TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes.


2 Answers

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.

like image 81
Ambuj Avatar answered Oct 17 '22 17:10

Ambuj


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

like image 4
Jean-Pierre Fouche Avatar answered Oct 17 '22 18:10

Jean-Pierre Fouche