使用TeamCity,我试图获得一个需要STA线程运行的(TestAutomationFX)测试。
它通过自定义app.config工作,配置NUnit 2.4.x(8)(由Gishu引用,谢谢,在http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.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,因此跑步者不尊重我的属性......
答案 0 :(得分:1)
TeamCity 4.0.1包含NUnit 2.5.0 beta 2.我认为应该适用于该情况。
答案 1 :(得分:0)
你能看出这有用吗?通过.config文件方法设置STA ...如在NUnit 2.5之前
http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html
答案 2 :(得分:0)
目前,我正在使用:
private void ForceSTAIfNecessary(ThreadStart threadStart)
{
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
threadStart();
else
CrossThreadTestRunner.RunInSTA(threadStart);
}
[Test]
public void TestRunApp()
{
ForceSTAIfNecessary(TestRunAppSTA);
}
public void TestRunAppSTA()
{
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
...
}
而不是:
[RequiresSTA]
public void TestRunAppSTA()
{
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
...
}