使用 TeamCity,我试图获得一个需要 STA 线程才能运行的 (TestAutomationFX) 测试。
它通过配置 NUnit 2.4.x (8) 的自定义 app.config 工作(如 Gishu 所指,谢谢,描述于http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-全站.html )
它通过以下方式工作:
/// <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;
}
}
我希望使用内置的东西。所以 NUnit 2.5.0.8322(Beta 1)的 RequiresSTAAttribute 似乎很理想。它独立工作,但不能通过 TeamCity 工作,即使我试图通过以下方式强制解决问题:
<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />
文档说跑步者支持 2.5.0 alpha 4?(http://www.jetbrains.net/confluence/display/TCD4/NUnit+for+MSBuild)
可能回答了我自己的问题,2.5.0 Aplha 4 没有 RequiresSTAAttribute,因此跑步者不尊重我的属性......