From 7bb9571e781e0651de12be08687c98076e58af23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 7 Jul 2026 05:50:53 +0200 Subject: [PATCH] Fix GlobalTestInitialize/Cleanup implementation gaps (#9662) - Remove incorrect claim that [Timeout] is unsupported on [GlobalTestInitialize] methods (it is supported and takes precedence over RunSettings, same as other fixtures). - Lower MSTEST0050 severity from Error to Warning to match peer fixture rules MSTEST0008-0013; record the change in AnalyzerReleases.Unshipped.md. - Remove the incorrect '-The class shouldn't be static' bullet from the MSTEST0050 description; a [TestClass] static class is valid (per WhenGlobalTestInitializeInStaticClass_NoDiagnostic). Regenerated xlf files. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../MSTest.Analyzers/AnalyzerReleases.Unshipped.md | 6 ++++++ .../GlobalTestFixtureShouldBeValidAnalyzer.cs | 2 +- src/Analyzers/MSTest.Analyzers/Resources.resx | 1 - src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf | 3 +-- src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf | 3 +-- .../Initialization/GlobalTestInitializeAttribute.cs | 2 +- 17 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md b/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md index 194e94a0dd..7c64e861f5 100644 --- a/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md +++ b/src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md @@ -12,3 +12,9 @@ MSTEST0067 | Performance | Disabled | AvoidThreadSleepAndTaskWaitInTestsAnalyzer MSTEST0068 | Usage | Info | CollectionAssertToAssertAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0068) MSTEST0070 | Usage | Warning | MemberConditionShouldBeValidAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0070) MSTEST0071 | Usage | Info | RedundantTestMethodDisplayNameAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0071) + +### Changed Rules + +Rule ID | New Category | New Severity | Old Category | Old Severity | Notes +--------|--------------|--------------|--------------|--------------|------- +MSTEST0050 | Usage | Warning | Usage | Error | GlobalTestFixtureShouldBeValidAnalyzer diff --git a/src/Analyzers/MSTest.Analyzers/GlobalTestFixtureShouldBeValidAnalyzer.cs b/src/Analyzers/MSTest.Analyzers/GlobalTestFixtureShouldBeValidAnalyzer.cs index f7b4a5c8a8..314ead869f 100644 --- a/src/Analyzers/MSTest.Analyzers/GlobalTestFixtureShouldBeValidAnalyzer.cs +++ b/src/Analyzers/MSTest.Analyzers/GlobalTestFixtureShouldBeValidAnalyzer.cs @@ -24,7 +24,7 @@ public sealed class GlobalTestFixtureShouldBeValidAnalyzer : DiagnosticAnalyzer new LocalizableResourceString(nameof(Resources.GlobalTestFixtureShouldBeValidMessageFormat), Resources.ResourceManager, typeof(Resources)), new LocalizableResourceString(nameof(Resources.GlobalTestFixtureShouldBeValidDescription), Resources.ResourceManager, typeof(Resources)), Category.Usage, - DiagnosticSeverity.Error, + DiagnosticSeverity.Warning, isEnabledByDefault: true); /// diff --git a/src/Analyzers/MSTest.Analyzers/Resources.resx b/src/Analyzers/MSTest.Analyzers/Resources.resx index eea9086975..a50b1409ae 100644 --- a/src/Analyzers/MSTest.Analyzers/Resources.resx +++ b/src/Analyzers/MSTest.Analyzers/Resources.resx @@ -784,7 +784,6 @@ The type declaring these methods should also respect the following rules: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. {Locked="[GlobalTestInitialize]"}{Locked="[GlobalTestCleanup]"}{Locked="[TestClass]"}{Locked="TestContext"}{Locked="ValueTask"}{Locked="Task"}{Locked="public"}{Locked="static"}{Locked="void"}{Locked="async void"}{Locked="async"}{Locked="class"} diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf index b55587baca..1b94509452 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf @@ -561,10 +561,9 @@ Typ deklarující tyto metody by měl také respektovat následující pravidla: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Aby byly metody s označením [GlobalTestInitialize] nebo [GlobalTestCleanup] platné, musí se řídit následujícím rozložením: + Aby byly metody s označením [GlobalTestInitialize] nebo [GlobalTestCleanup] platné, musí se řídit následujícím rozložením: – Nesmí být deklarované pro obecnou class. – Musí být „public“. – Musí být „static“. diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf index 753ec9af25..d7b15646e7 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf @@ -562,10 +562,9 @@ Der Typ, der diese Methoden deklariert, sollte auch die folgenden Regeln beachte The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Methoden, die mit „[GlobalTestInitialize]“ oder „[GlobalTestCleanup]“ gekennzeichnet sind, sollten dem folgenden Layout folgen, um gültig zu sein: + Methoden, die mit „[GlobalTestInitialize]“ oder „[GlobalTestCleanup]“ gekennzeichnet sind, sollten dem folgenden Layout folgen, um gültig zu sein: – kann nicht für eine generische class deklariert werden – muss „public“ sein – muss „static“ sein diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf index 4e8b0b7e06..c6b2d751ea 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf @@ -561,10 +561,9 @@ El tipo que declara estos métodos también debe respetar las reglas siguientes: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Los métodos marcados con '[GlobalTestInitialize]' o '[GlobalTestCleanup]' deben seguir el siguiente diseño para ser válidos: + Los métodos marcados con '[GlobalTestInitialize]' o '[GlobalTestCleanup]' deben seguir el siguiente diseño para ser válidos: -no se puede declarar en una class genérica - debería ser 'public' - debería estar 'static' diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf index 6f793ce2ae..fff712eb7b 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf @@ -561,10 +561,9 @@ Le type doit être une classe The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Les méthodes marquées par « [GlobalTestInitialize] » ou « [GlobalTestCleanup] » doivent respecter le schéma suivant pour être valides : + Les méthodes marquées par « [GlobalTestInitialize] » ou « [GlobalTestCleanup] » doivent respecter le schéma suivant pour être valides : - elle ne peut pas être déclarée dans une classe générique – elle doit être « public » – elle doit être « static » diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf index b19497f328..16fe14405f 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf @@ -561,10 +561,9 @@ Anche il tipo che dichiara questi metodi deve rispettare le regole seguenti: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - I metodi contrassegnati con ‘[GlobalTestInitialize]’ o ‘[GlobalTestCleanup]’ devono seguire il layout seguente per essere validi: + I metodi contrassegnati con ‘[GlobalTestInitialize]’ o ‘[GlobalTestCleanup]’ devono seguire il layout seguente per essere validi: - Non può essere dichiarato in una classe generica - Deve essere 'public' - Deve essere 'static' diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf index 7e3d93dcbf..5354086e50 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf @@ -561,10 +561,9 @@ The type declaring these methods should also respect the following rules: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - '[GlobalTestInitialize]' または '[GlobalTestCleanup]' でマークされたメソッドを有効にするには、次のレイアウトに従う必要があります: + '[GlobalTestInitialize]' または '[GlobalTestCleanup]' でマークされたメソッドを有効にするには、次のレイアウトに従う必要があります: - ジェネリック class で宣言することはできません - 'public' である必要があります - 'static' である必要があります diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf index 40795ee6d0..5a5f7860b9 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf @@ -561,10 +561,9 @@ The type declaring these methods should also respect the following rules: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - '[GlobalTestInitialize]' 또는 '[GlobalTestCleanup]'으로 표시된 메서드는 다음 레이아웃을 따라야 유효합니다. + '[GlobalTestInitialize]' 또는 '[GlobalTestCleanup]'으로 표시된 메서드는 다음 레이아웃을 따라야 유효합니다. - 제네릭 class에서 선언할 수 없습니다. - 'public'이어야 합니다. - 'static'이어야 합니다. diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf index 966ad4cebc..10765d17fb 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf @@ -561,10 +561,9 @@ Typ deklarujący te metody powinien również przestrzegać następujących regu The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Metody oznaczone znakiem „[GlobalTestInitialize]” lub „[GlobalTestCleanup]” powinny być zgodne z następującym układem, aby były prawidłowe: + Metody oznaczone znakiem „[GlobalTestInitialize]” lub „[GlobalTestCleanup]” powinny być zgodne z następującym układem, aby były prawidłowe: — nie może być zadeklarowana w class ogólnej — powinna być typu „public” — powinna mieć wartość „static” diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf index 6414281d2f..081e22f28b 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf @@ -561,10 +561,9 @@ O tipo que declara esses métodos também deve respeitar as seguintes regras: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Os métodos marcados com "[GlobalTestInitialize]" ou "[GlobalTestCleanup]" devem seguir o seguinte layout para serem válidos: + Os métodos marcados com "[GlobalTestInitialize]" ou "[GlobalTestCleanup]" devem seguir o seguinte layout para serem válidos: -não podem ser declarados em uma classe genérica -devem ser "public" -devem ser "static" diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf index 6c6a98a766..637d2e61a7 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf @@ -567,10 +567,9 @@ The type declaring these methods should also respect the following rules: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - Чтобы метод, помеченный "[GlobalTestInitialize]" или "[GlobalTestCleanup]", был допустимым, он должен соответствовать следующей структуре: + Чтобы метод, помеченный "[GlobalTestInitialize]" или "[GlobalTestCleanup]", был допустимым, он должен соответствовать следующей структуре: – не может быть объявлен для универсального class ("generic") – должен быть общедоступным ("public") – должен быть статическим ("static") diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf index d7a1a2e960..9a6ba15b24 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf @@ -561,10 +561,9 @@ Bu yöntemleri bildiren tipin ayrıca aşağıdaki kurallara uyması gerekir: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - ‘[GlobalTestInitialize]’ veya ‘[GlobalTestCleanup]’ ile işaretlenmiş yöntemlerin geçerli olabilmesi için şu düzeni izlemesi gerekir: + ‘[GlobalTestInitialize]’ veya ‘[GlobalTestCleanup]’ ile işaretlenmiş yöntemlerin geçerli olabilmesi için şu düzeni izlemesi gerekir: -genel bir class tanımlanamaz -'public' olmalıdır -'static' olmalıdır diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf index 534abf6eaa..77ea98c163 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf @@ -561,10 +561,9 @@ The type declaring these methods should also respect the following rules: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - 标记有 "[GlobalTestInitialize]" 或 "[GlobalTestCleanup]" 的方法应遵循以下布局才会有效: + 标记有 "[GlobalTestInitialize]" 或 "[GlobalTestCleanup]" 的方法应遵循以下布局才会有效: - 不能在泛型 class 上声明它 - 它应为 "public" - 它应为 "static" diff --git a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf index cb33c7623d..95542115d5 100644 --- a/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf +++ b/src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf @@ -561,10 +561,9 @@ The type declaring these methods should also respect the following rules: The type declaring these methods should also respect the following rules: -The type should be a class -The class should be 'public' --The class shouldn't be 'static' -The class should be marked with '[TestClass]' (or a derived attribute) -the class should not be generic. - 標示為 '[GlobalTestInitialize]' 或 '[GlobalTestCleanup]' 的方法應該遵循下列配置才能有效: + 標示為 '[GlobalTestInitialize]' 或 '[GlobalTestCleanup]' 的方法應該遵循下列配置才能有效: -其不能在泛型 class 上宣告 -其應為 'public' -其應為 'static' diff --git a/src/TestFramework/TestFramework/Attributes/Lifecycle/Initialization/GlobalTestInitializeAttribute.cs b/src/TestFramework/TestFramework/Attributes/Lifecycle/Initialization/GlobalTestInitializeAttribute.cs index ae7b0be236..f6d695830c 100644 --- a/src/TestFramework/TestFramework/Attributes/Lifecycle/Initialization/GlobalTestInitializeAttribute.cs +++ b/src/TestFramework/TestFramework/Attributes/Lifecycle/Initialization/GlobalTestInitializeAttribute.cs @@ -8,7 +8,7 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// The method to which this attribute is applied must be public, static, non-generic, has a single parameter of type TestContext, and either returns void or a Task. /// /// -/// Multiple methods with this attribute in the assembly is allowed, but there is no guarantee of the order in which they will be executed. In addition, TimeoutAttribute isn't supported on methods with this attribute. +/// Multiple methods with this attribute in the assembly is allowed, but there is no guarantee of the order in which they will be executed. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public sealed class GlobalTestInitializeAttribute : Attribute;