views:

632

answers:

3

Using TeamCity, I'm trying to get a (TestAutomationFX) test that requires an STA thread to run .

It works via a custom app.config that configures NUnit 2.4.x (8) (as referred to by Gishu, thanks, described at http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html)

It works via:

/// <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;
    }
}

I'm hoping to use something built in. So NUnit 2.5.0.8322 (Beta 1)'s RequiresSTAAttribute seems ideal. It works standalone, but not via TeamCity, even when I attempt to force the issue via:

<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />

The docs say the runner supports 2.5.0 alpha 4? (http://www.jetbrains.net/confluence/display/TCD4/NUnit+for+MSBuild)

Probably answering my own question, 2.5.0 Aplha 4 doesnt have RequiresSTAAttribute, hence the runner is not honouring my Attribute...

A: 

Can you see if this helps? Setting STA via the .config file approach... as in pre NUnit 2.5

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

Gishu
A: 

For now, I'm using:

    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));
        ...
    }

instead of:

    [RequiresSTA]
    public void TestRunAppSTA()
    {
        Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
        ...
    }
Ruben Bartelink
+1  A: 

TeamCity 4.0.1 contains NUnit 2.5.0 beta 2. I believe that should work for that case.

Eugene Petrenko
I take it you mean Beta 1 - build 8322 which is the latest build on nunit.org (now on 090105 and was when I posted initialy in 0812)
Ruben Bartelink
BTW it all works now (using NH 2.5 Beta 1 with TC 4.0.1)(and s/initialy/initially/ - why cant we edit comments!)
Ruben Bartelink