2

使用 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,因此跑步者不尊重我的属性......

4

3 回答 3

1

TeamCity 4.0.1 包含 NUnit 2.5.0 beta 2。我相信这应该适用于这种情况。

于 2008-12-29T11:55:13.880 回答
0

你看看这是否有帮助?通过 .config 文件方法设置 STA ......就像在 NUnit 2.5 之前一样

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

于 2008-12-05T11:13:07.760 回答
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 回答