Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'..

like image 836
TobiMcNamobi Avatar asked Feb 07 '13 11:02

TobiMcNamobi


4 Answers

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

like image 176
TobiMcNamobi Avatar answered Nov 20 '22 16:11

TobiMcNamobi


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;
}
like image 44
Zain Rizvi Avatar answered Nov 20 '22 17:11

Zain Rizvi


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.

like image 1
Mårten Avatar answered Nov 20 '22 16:11

Mårten


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
like image 1
Jeremy Thompson Avatar answered Nov 20 '22 16:11

Jeremy Thompson