Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

STAThread
WesleyCanneyt
#1 Posted : Wednesday, June 8, 2022 11:56:52 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/9/2022(UTC)
Posts: 6
Location: Belgium

Was thanked: 1 time(s) in 1 post(s)
Hello

We have a big project in WPF C#.

We have some legacy code that can't be refactored in such a way that we can run "normal" tests.
The test we can build at this point need to run in STA Thread.
Our tests runs perfectly with mstest but not with NCrunch.

On MSTest we customize the attribute and use the following classes.

Is there a solution for this problem?

Thank you

public class STATestClassAttribute : TestClassAttribute
{
public override TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute testMethodAttribute)
{
if (testMethodAttribute is STATestMethodAttribute)
return testMethodAttribute;

return new STATestMethodAttribute(base.GetTestMethodAttribute(testMethodAttribute));
}
}

public class STATestMethodAttribute : TestMethodAttribute
{
private readonly TestMethodAttribute _testMethodAttribute;

public STATestMethodAttribute()
{
}

public STATestMethodAttribute(TestMethodAttribute testMethodAttribute)
{
_testMethodAttribute = testMethodAttribute;
}

public override TestResult[] Execute(ITestMethod testMethod)
{
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
return Invoke(testMethod);

TestResult[] result = null;
var thread = new Thread(() => result = Invoke(testMethod));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return result;
}

private TestResult[] Invoke(ITestMethod testMethod)
{
if (_testMethodAttribute != null)
return _testMethodAttribute.Execute(testMethod);

return new[] { testMethod.Invoke(null) };
}
}

The error message in ncrunch:
System.InvalidOperationException: The calling thread must be STA, because many UI components require this.
at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.InputManager.get_Current()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Panel..ctor()
at System.Windows.Controls.Canvas..ctor()
Remco
#2 Posted : Wednesday, June 8, 2022 12:11:18 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for posting.

Can you please check this setting? MS Test thread apartment state.

Given that NCrunch's MSTest runner should default to STAThread, I'm wondering if there might be something else involved here. Does the call stack on the exception show the thread involved being from NCrunch calling the test method? If not, there might be something else going on in the environment here.

Unfortunately the NCrunch runner for MSTest doesn't support custom attributes, as we can't call this code without deeper integration with MSTest itself (sadly not a practical option for us).
WesleyCanneyt
#3 Posted : Wednesday, June 8, 2022 12:17:40 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/9/2022(UTC)
Posts: 6
Location: Belgium

Was thanked: 1 time(s) in 1 post(s)
Hi,

Thank you for the reply.
Can we use another framework (for this particular problem) for this test project that works with ncrunch?
NUnit for example?
WesleyCanneyt
#4 Posted : Wednesday, June 8, 2022 1:10:11 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 2/9/2022(UTC)
Posts: 6
Location: Belgium

Was thanked: 1 time(s) in 1 post(s)
Hello

I've tried NUnit and everythings works as expected now.
In Test explorer and ncrunch.

Thank you.
1 user thanked WesleyCanneyt for this useful post.
Remco on 6/8/2022(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.047 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download