Utilisation de NUnit-2.5 Nécessite l'attribut STAAttribute avec TeamCity 4
-
19-08-2019 - |
Question
À l'aide de TeamCity, j'essaie de réaliser un test (TestAutomationFX) nécessitant l'exécution d'un thread STA.
Cela fonctionne via un app.config personnalisé qui configure NUnit 2.4.x (8) (comme indiqué par Gishu, merci, décrit à http://madcoderspeak.blogspot.com/2008/12/getting-nunit-to-go-all-sta.html )
Cela fonctionne 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;
}
}
J'espère utiliser quelque chose d'intégré. Donc, NSTP requiert un attribut AAAA de la version 2.5.0.8322 (version bêta 1) qui semble idéal. Cela fonctionne de manière autonome, mais pas via TeamCity, même lorsque je tente d'imposer le problème via:
<NUnit Assemblies="Test\bin\$(Configuration)\Test.exe" NUnitVersion="NUnit-2.5.0" />
La documentation indique que le coureur prend en charge la version 2.5.0 alpha 4? ( http://www.jetbrains.net/confluence/display/TCD4 / NUnit + pour + MSBuild )
Répondant probablement à ma propre question, 2.5.0 Aplha 4 n'a pas de requinstaSTAAattribut, le coureur ne respecte donc pas mon attribut ...
La solution
TeamCity 4.0.1 contient NUnit 2.5.0 bêta 2. Je pense que cela devrait fonctionner dans ce cas.
Autres conseils
Pouvez-vous voir si cela aide? Configuration de STA via l’approche du fichier .config ... comme dans la version antérieure à NUnit 2.5
http: //madcoderspeak.blogspot .com / 2008/12 / Obtenir-une-unité à aller-tout-sta.html
Pour l'instant, j'utilise:
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));
...
}
au lieu de:
[RequiresSTA]
public void TestRunAppSTA()
{
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
...
}