L'uso di NUnit-2.5 richiede STAAttribute con TeamCity 4
-
19-08-2019 - |
Domanda
Utilizzando TeamCity, sto cercando di ottenere un test (TestAutomationFX) che richiede l'esecuzione di un thread STA.
Funziona tramite un'app.config personalizzata che configura NUnit 2.4.x (8) (come indicato da Gishu, grazie, descritto in http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html )
Funziona tramite:
/// <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;
}
}
Spero di usare qualcosa di integrato. Quindi, NSit 2.5.0.8322 (Beta 1) richiedeSTAAttribute sembra l'ideale. Funziona da solo, ma non tramite TeamCity, anche quando provo a forzare il problema tramite:
<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />
I documenti dicono che il corridore supporta 2.5.0 alpha 4? ( http://www.jetbrains.net/confluence/display/TCD4 / NUnit + per + MSBuild )
Probabilmente rispondendo alla mia stessa domanda, 2.5.0 Aplha 4 non ha il RequisitoSTAAttributo, quindi il corridore non sta onorando il mio Attributo ...
Soluzione
TeamCity 4.0.1 contiene NUnit 2.5.0 beta 2. Credo che dovrebbe funzionare in questo caso.
Altri suggerimenti
Riesci a vedere se questo aiuta? Impostazione STA tramite l'approccio al file .config ... come in NUnit 2.5
precedentehttp: //madcoderspeak.blogspot .com / 2008/12 / getting-NUnit-to-go-all-sta.html
Per ora, sto usando:
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));
...
}
anziché:
[RequiresSTA]
public void TestRunAppSTA()
{
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
...
}