2

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 がないため、ランナーは私の属性を尊重していません...

4

3 に答える 3

1

TeamCity4.0.1にはNUnit2.5.0ベータ2が含まれています。その場合はそれでうまくいくはずです。

于 2008-12-29T11:55:13.880 に答える
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));
        ...
    }
于 2008-12-05T11:39:22.947 に答える
0

これが役立つかどうかわかりますか?NUnit 2.5より前のように、.configファイルアプローチを介してSTAを設定します

http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html

于 2008-12-05T11:13:07.760 に答える