سؤال

وعن طريق TeamCity، وأنا أحاول الحصول على (TestAutomationFX) الاختبار الذي يتطلب وجود موضوع STA لتشغيل.

وكان يعمل عن طريق app.config المخصصة التي بتكوين NUnit 2.4.X (8) (على النحو المشار إليه من قبل جيشو، وذلك بفضل وصفت في <لأ href = "http://madcoderspeak.blogspot.com/2008/12/ الحصول على-nunit-للذهاب للجميع sta.html "يختلط =" نوفولو noreferrer "> 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 ألفا 4؟ ( http://www.jetbrains.net/confluence/display/TCD4 / NUnit + ل+ MSBuild )

والإجابة ربما سؤالي الخاصة، 2.5.0 Aplha 4 لا يتلقى RequiresSTAAttribute، وبالتالي عداء لا تكريم لي السمة ...

هل كانت مفيدة؟

المحلول

وTeamCity 4.0.1 يحتوي NUnit 2.5.0 بيتا 2. وأعتقد أنه ينبغي العمل من أجل هذه القضية.

نصائح أخرى

هل يمكن ان نرى ما اذا كان هذا يساعد؟ وضع STA عبر نهج ملف .config ... كما في مرحلة ما قبل NUnit 2.5

HTTP: //madcoderspeak.blogspot كوم / 2008/12 /-الحصول على nunit للذهاب للجميع 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));
        ...
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top