testfx: [3.0 regression] Unable to run a test with timeout in STA ApartmentState

Describe the bug

MSTest v2 ran all tests in STA ApartmentState when executed in a .NET Framework runtime. v3 runs “normal” tests still in STA, but tests that have a TimeoutAttribute applied now run in MTA.

Steps To Reproduce

Run these tests with various versions of MSTest and runtimes:

[TestMethod]
public void TestMethod1()
{
    // Outputs:
    // MSTest   net7.0   net48
    // 2.2.10   MTA      STA
    // 3.0.2    MTA      STA
    Console.WriteLine(Thread.CurrentThread.GetApartmentState());
}

[TestMethod, Timeout(1000)]
public void TestMethod2()
{
    // Outputs:
    // MSTest   net7.0   net48
    // 2.2.10   MTA      STA
    // 3.0.2    MTA      MTA    <-- Problem
    Console.WriteLine(Thread.CurrentThread.GetApartmentState());
}

Expected behavior

Either MSTest v2 behavior should be retained, or I should have a way to force STA on an unit test.

Actual behavior

Tests with timeout always run in MTA.

Additional context

I need STA to run UI (WPF) tests. Those tests try to execute WPF commands which now fail with InvalidOperationException: The calling thread must be STA, because many UI components require this.

This is most likely a side effect of #1296 The package “MSTest.STAExtensions” which is mentioned in #21 does not help/work, see saikrishnav/testfxSTAext#5 Related: #320

AB#1822144

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@cremor we have reverted the change and will include the fix in 3.0.3 (we hope to ship it next week).

@fforjan I believe this revert won’t be enough for you as you would want to run some tests with STA using .NET 6 and this won’t be possible (it was not possible in previous versions of MSTest). I have created #1674 to track providing a good solution for this use case.

Hi @fforjan,

Sorry it’s taking some time but we are exploring the solution to fix this issue. If we don’t find something reasonable quickly we will probably revert the linked change in a bug fix version and plan to reapply the change in a later update when we have a proper upgrade path for users.

Hi @cremor we will discuss it and let you as soon as possible

But the default behavior worked in 2.2.10. So it was an intentional change in v3 to have different ApartmentStates when running with or without a TimeoutAttribute?