Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a ClassInitialize decorated method making all my tests fail?

I understand, from MSDN, that ClassInitialize is to mark a method that will do setup code for all tests, once, before all tests run. When I include such a method in the abridged fixture below, all tests fail. As soon as I comment it out, they pass again.

[TestClass]
public class AuthenticationTests
{
    [ClassInitialize]
    public void SetupAuth()
    {
        var x = 0;
    }

    [TestMethod]
    public void TestRegisterMemberInit()
    {
        Assert.IsTrue(true);
    }
}
like image 475
ProfK Avatar asked Jul 02 '12 16:07

ProfK


1 Answers

The [ClassInitialize] decorated method should be static and take exactly one parameter of type TestContext:

[ClassInitialize]
public static void SetupAuth(TestContext context)
{
    var x = 0;
}

In fact, if I copy-paste your code into a clean VS project, the testrunner explains exactly that in the error message:

Method UnitTestProject1.AuthenticationTests.SetupAuth has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext.

like image 159
driis Avatar answered Sep 23 '22 19:09

driis