Usando NUnit-2,5 RequiresSTAAttribute com TeamCity 4
-
19-08-2019 - |
Pergunta
Usando TeamCity, eu estou tentando obter um teste (TestAutomationFX) que requer um segmento STA para executar.
Ele funciona através de um costume app.config que configura NUnit 2.4.x (8) (tal como referido por Gishu, graças, descrito em http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html )
Ele funciona 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;
}
}
Estou esperando para usar algo construído. Então NUnit 2.5.0.8322 (Beta 1) 's RequiresSTAAttribute parece ideal. Ele funciona de forma autónoma, mas não através TeamCity, mesmo quando eu tentar forçar a questão via:
<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />
Os documentos dizem o corredor suporta 2.5.0 alpha 4? ( http://www.jetbrains.net/confluence/display/TCD4 / NUnit + para + MSBuild )
Provavelmente responder a minha própria pergunta, 2.5.0 Aplha 4 esquentar têm RequiresSTAAttribute, daí o corredor não está honrando meu Atributo ...
Solução
TeamCity 4.0.1 contém NUnit 2.5.0 beta 2. Eu acredito que deve funcionar para esse caso.
Outras dicas
Você pode ver se isso ajuda? Definir STA através da abordagem arquivo.config ... como em pré NUnit 2.5
http: //madcoderspeak.blogspot .com / 2008/12 / obtendo-nunit-to-go-all-sta.html
Por enquanto, eu estou 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));
...
}
em vez de:
[RequiresSTA]
public void TestRunAppSTA()
{
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
...
}