Using TeamCity, I'm trying to get a (TestAutomationFX) test that requires an STA thread to run .
It works via a custom app.config that configures NUnit 2.4.x (8) (as referred to by Gishu, thanks, described at http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html)
It works via:
/// <summary>
/// Via Peter Provost / http://www.hedgate.net/articles/2007/01/08/instantiating-a-wpf-control-from-an-nunit-test/
/// </summary>
public static class CrossThreadTestRunner // To be replaced with (RequiresSTA) from NUnit 2.5
{
public static void RunInSTA(Action userDelegate)
{
Exception lastException = null;
Thread thread = new Thread(delegate()
{
try
{
userDelegate();
}
catch (Exception e)
{
lastException = e;
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
if (lastException != null)
ThrowExceptionPreservingStack(lastException);
}
[ReflectionPermission(SecurityAction.Demand)]
static void ThrowExceptionPreservingStack(Exception exception)
{
FieldInfo remoteStackTraceString = typeof(Exception).GetField(
"_remoteStackTraceString",
BindingFlags.Instance | BindingFlags.NonPublic);
remoteStackTraceString.SetValue(exception, exception.StackTrace + Environment.NewLine);
throw exception;
}
}
I'm hoping to use something built in. So NUnit 2.5.0.8322 (Beta 1)'s RequiresSTAAttribute seems ideal. It works standalone, but not via TeamCity, even when I attempt to force the issue via:
<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />
The docs say the runner supports 2.5.0 alpha 4? (http://www.jetbrains.net/confluence/display/TCD4/NUnit+for+MSBuild)
Probably answering my own question, 2.5.0 Aplha 4 doesnt have RequiresSTAAttribute, hence the runner is not honouring my Attribute...