TeamCity 4でNUnit-2.5 RequiresSTAAttributeを使用する
-
19-08-2019 - |
質問
TeamCityを使用して、実行するSTAスレッドを必要とする(TestAutomationFX)テストを取得しようとしています。
NUnit 2.4.x(8)を構成するカスタムapp.configを介して動作します(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(ベータ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がないため、ランナーは私の属性を尊重していません...
解決
TeamCity 4.0.1には、NUnit 2.5.0ベータ2が含まれています。その場合は動作するはずです。
他のヒント
これが役立つかどうかわかりますか? .configファイルアプローチによるSTAの設定... NUnit 2.5より前の場合
http://madcoderspeak.blogspot .com / 2008/12 / getting-nunit-to-go-all-sta.html
今のところ、私は使用しています:
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));
...
}