Build/Test Issues

STAThread

Started by WesleyCanneyt on 2,855 views

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()

Edited

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

Edited

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?

Edited

Hello

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

Thank you.

Post a reply

Log in to reply.