Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 888
Random Avatar asked Oct 02 '22 02:10

Random


2 Answers

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
like image 127
Random Avatar answered Oct 21 '22 19:10

Random


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.
like image 44
Mårten Avatar answered Oct 21 '22 19:10

Mårten