Usar NUnit-2.5 RequiereSTAAttribute con TeamCity 4
-
19-08-2019 - |
Pregunta
Utilizando TeamCity, estoy tratando de obtener una prueba (TestAutomationFX) que requiere un subproceso STA para ejecutarse.
Funciona a través de una aplicación personalizada.config que configura NUnit 2.4.x (8) (según lo mencionado por Gishu, gracias, descrito en http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html )
Funciona a través de:
/// <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;
}
}
Espero usar algo integrado. Por lo tanto, NUnit 2.5.0.8322 (Beta 1) requiere el SATAAttribute ideal. Funciona de manera independiente, pero no a través de TeamCity, incluso cuando intento forzar el problema a través de:
<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />
¿Los documentos dicen que el corredor admite 2.5.0 alpha 4? ( http://www.jetbrains.net/confluence/display/TCD4 / NUnit + para + MSBuild )
Probablemente respondiendo mi propia pregunta, 2.5.0 Aplha 4 no tiene RequiereSTAAttribute, por lo tanto, el corredor no está honrando mi Atributo ...
Solución
TeamCity 4.0.1 contiene NUnit 2.5.0 beta 2. Creo que debería funcionar para ese caso.
Otros consejos
¿Puedes ver si esto ayuda? Configuración de STA a través del enfoque de archivo .config ... como en pre NUnit 2.5
http: //madcoderspeak.blogspot .com / 2008/12 / getting-nunit-to-go-all-sta.html
Por ahora, estoy 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));
...
}
en lugar de:
[RequiresSTA]
public void TestRunAppSTA()
{
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
...
}