TobiMcNamobi
TobiMcNamobi

Reputation: 4823

What does "Method ...ClassInitialize has wrong signature ..." mean?

In my Visual Studio 2012 solution I have a C# project for unit testing C++/CLI code, e.g.

...
using System.IO;
using Stuff;

namespace MyCLIClassTest
{
    [TestClass]
    public class MyCLIClassTest
    {
        public MyCLIClassTest() {}

        [ClassInitialize]
        public static void Setup(TestContext testContext) 
        {
        }

        [TestMethod]
        public void LibraryAccessTest()
        {
            ...
        }
    }
}

Now, the C# tests all fail with a message like "Method MyCLIClassTest.MyCLIClassTest.ClassInitialize has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext."

After removing the ClassInitializer I got "Unable to set TestContext property for the class MyCLIClassTest.MyCLIClassTest. Error: System.ArgumentException: Object of type 'Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestContextImplementation' cannot be converted to type 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext'..

Upvotes: 23

Views: 23105

Answers (4)

Jeremy Thompson
Jeremy Thompson

Reputation: 65712

Setup has wrong signature. Parameter 1 should be of type Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.

I was running a Load Test Project and had both v10.0.0.0 versions of the DLLs:

Microsoft.VisualStudio.QualityTools.LoadTestFramework.dll
Microsoft.VisualStudio.QualityTools.WebTestFramework.dll

Changing the version LoadTestFramework to version 10.1 didn't fix it.

I had to goto my Unit Test Project and delete the MSTest.Adapter references:

Microsoft.VisualStudio.TestPlatform.TestFramework.dll
Microsoft.VisualStudio.TestPlatform.Extensions.dll

Then in the Unit Test Project add a reference to the v10.1

Microsoft.VisualStudio.QualityTools.LoadTestFramework.dll

Upvotes: 1

TobiMcNamobi
TobiMcNamobi

Reputation: 4823

I used DLLs of older unit testing framework versions. This happened because the project migrated recently to VS2012.

So, in the solution explorer under the test project you find "References". Right click it and select "Add reference..." to open the Reference Manager. Search for "unittest" and check the Microsoft.VisualStudio.QualityTools.UnitTestFramework with version number 10.1.0.0. Un-check all other versions of this assembly. Close the manager by clicking OK.

enter image description here

Upvotes: 26

Zain Rizvi
Zain Rizvi

Reputation: 24656

An alternate answer copied from a duplicate question: Why is a ClassInitialize decorated method making all my tests fail?

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;
}

Upvotes: 16

Mårten
Mårten

Reputation: 349

I had the exact same issue and removing/adding references as suggested by TobiMcNamobi did not solve it for me, however removing the reference, right click the project and selecting "Add > Unit test..." and thereby getting the reference re-generated worked. Not sure what the difference was compared to doing it manually.

Upvotes: 1

Related Questions