Skip to content

Latest commit

 

History

History
141 lines (106 loc) · 4.23 KB

File metadata and controls

141 lines (106 loc) · 4.23 KB

PH2162: TestTimeouts class should be internal

Property Value
Package Philips.CodeAnalysis.MsTestAnalyzers
Diagnostic ID PH2162
Category MsTest
Analyzer TestTimeoutsClassAccessibilityAnalyzer
CodeFix Yes
Severity Warning
Enabled By Default Yes

Introduction

Projects commonly define a TestTimeouts class that centralizes timeout constants (e.g., CiAppropriate, Integration). This analyzer enforces that any class named TestTimeouts is declared as internal and not static. Public exposure or use of static on this coordination class is discouraged.

This rule helps avoid conflicts with the MSTest analyzer that restricts all classes in a test project from being public unless they are annotated with [TestClass].

Reason

Keeping TestTimeouts internal:

  1. Prevents leaking test-only infrastructure into the public API surface of a library.
  2. Avoids external projects taking a compile-time dependency on timeout values that are intended to be adjusted freely.
  3. Encourages each assembly to own and tune its own timeout set without cross-assembly coupling.
  4. Reduces the risk that production code (outside test projects) references test timing constants.

Disallowing static (while permitting sealed) helps future flexibility (e.g., partial conditional compilation patterns) and keeps the shape consistent with existing guidance in related timeout analyzers. A sealed internal class with only const members satisfies the same usage needs without requiring static.

What triggers the diagnostic

The diagnostic is reported for any class whose identifier is exactly TestTimeouts if either:

  • It is declared public (with or without sealed).
  • It is declared internal static.

The class name comparison is exact and case-sensitive.

How to fix

Change the declaration to be internal (optionally sealed) and remove static if present. The provided Code Fix will:

  • Remove public.
  • Remove static (when paired with internal).
  • Insert internal if missing.
  • Preserve sealed if it exists.

Examples

Code that triggers the diagnostic (public):

public class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Fix:

internal class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Code that triggers the diagnostic (public sealed):

public sealed class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Fix (Code Fix output):

internal sealed class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Code that triggers the diagnostic (internal static):

internal static class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Fix (Code Fix output):

internal class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Code that does NOT trigger the diagnostic:

internal class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

internal sealed class TestTimeouts
{
	public const int CiAppropriate = 1000;
}

Other class names are ignored:

public class SomeOtherClass
{
	public const int CiAppropriate = 1000;
}

Code Fix behavior

Selecting the Code Fix on a reported diagnostic rewrites the class modifiers as described above. It does not otherwise reorder members or alter whitespace beyond necessary modifier changes.

Similar analyzers

ID Title
PH2012 Test has Timeout
PH2015 Test must have appropriate Test Category

These analyzers collectively encourage consistent timeout usage patterns.

Configuration

This analyzer does not currently support custom configuration keys. Standard suppression mechanisms (pragma, GlobalSuppressions.cs, or EditorConfig severity adjustments) apply, but suppression is discouraged—prefer adopting the recommended accessibility.

# Example: adjust severity (not recommended)
dotnet_diagnostic.PH2162.severity = warning