Random
Random

Reputation: 4779

Fails to execute classes with ClassInitialize method present

I face an awkward situation - all test classes that have [ClassInitialize] method present fail to execute all test methods inside.

Example:

[TestClass]
public class ChargeAccountServiceTests
{
    private static PrivateType ChargeAccountService_Accessor;

    [ClassInitialize]
    public static void InitializeClass(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext context)
    {
         ChargeAccountService_Accessor = new PrivateType(typeof(ChargeAccountService));
    }

    [TestMethod]
    public void TestFixOMRHappySHA()
    {
          //TEST LOGIC
    }
}

Causes test agent to throw following exception:

Test Name:  TestFixOMRHappySHA
Test FullName:  ChargeAccountServiceTests.TestFixOMRHappySHA
Test Source:    \ChargeAccountServiceTests.cs : line 22
Test Outcome:   Failed
Test Duration:  0:00:00

Result Message:

Method ChargeAccountServiceTests.InitializeClass has wrong signature. Parameter 1 should be of type Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.

This test has been working few days ago. Test project target is .NET 3.5

Upvotes: 5

Views: 5206

Answers (3)

stuzor
stuzor

Reputation: 2402

I had the same error but for [AssemblyInitialize] not [ClassInitialize].

Removing the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework and re-adding v10.0.0.0 fixed the problem for me.

It turned out my different test projects were referencing different versions, and making them all consistently point to 10.0.0.0 fixed the problem.

Upvotes: 0

Mårten
Mårten

Reputation: 349

From my answer to a similar question, I had the same issue, for me it worked to

  1. Remove the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework
  2. Right click the project and select "Add > Unit test..." which restores the reference with the correct version.

Upvotes: 2

Random
Random

Reputation: 4779

Finally, I got resolved it, by applying combination of tips I found:

  1. One of MSTest project was targeted to 4.0 while others to 3.5 - so I had to change it and rebuild fakes.
  2. I removed Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll reference and added it again to all my test projects
  3. I found an old version of vsdmi file in my solution - removed
  4. I removed and created Local.testsettings
  5. Restarted VS

Upvotes: 2

Related Questions