Home » C# » MSTest and the error Method should be marked static

MSTest and the error Method should be marked static

If you came from the world of NUnit you may stomped into the error “Method should be marked static” without any other indication of which method it’s about.

With NUnit, you were using TestFixtureSetUp as attribute to a class’ method to get it executed before all test once. With MSTest you have to use the attribute ClassInitialize.

So, if you are used to write :

        [TestFixtureSetUp]
        public void Init()
        {
            //Some code executed once
        }

You cannot simply use :

        [ClassInitialize]
        public void Init()
        {
            //Some code executed once
        }

In fact, the Microsoft Unit Testing framework require to have this method public with a parameter of TestContext.

        [ClassInitialize]
        public static void Init(TestContext ctx)
        {
            //Some code executed once
        }

Once this is set as it should, with the statis method and the TestContext attribute, the error “Method should be marked static” will be removed and you will be all fine.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.