|
| 1 | +--- |
| 2 | +title: "MSTEST0052: Avoid passing an explicit 'DynamicDataSourceType' and use the default auto detect behavior" |
| 3 | +description: "Learn about code analysis rule MSTEST0052: Avoid passing an explicit 'DynamicDataSourceType' and use the default auto detect behavior" |
| 4 | +ms.date: 10/01/2025 |
| 5 | +f1_keywords: |
| 6 | +- MSTEST0052 |
| 7 | +- AvoidExplicitDynamicDataSourceTypeAnalyzer |
| 8 | +helpviewer_keywords: |
| 9 | +- AvoidExplicitDynamicDataSourceTypeAnalyzer |
| 10 | +- MSTEST0052 |
| 11 | +author: Youssef1313 |
| 12 | +ms.author: ygerges |
| 13 | +ai-usage: ai-generated |
| 14 | +--- |
| 15 | +# MSTEST0052: Avoid passing an explicit 'DynamicDataSourceType' and use the default auto detect behavior |
| 16 | + |
| 17 | +| Property | Value | |
| 18 | +|-------------------------------------|--------------------------------------------------------------------------------------------| |
| 19 | +| **Rule ID** | MSTEST0052 | |
| 20 | +| **Title** | Avoid passing an explicit 'DynamicDataSourceType' and use the default auto detect behavior | |
| 21 | +| **Category** | Usage | |
| 22 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 23 | +| **Enabled by default** | Yes | |
| 24 | +| **Default severity** | Warning | |
| 25 | +| **Introduced in version** | 3.11.0 | |
| 26 | +| **Is there a code fix** | Yes | |
| 27 | + |
| 28 | +## Cause |
| 29 | + |
| 30 | +A <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute> explicitly specifies <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Property> or <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Method> instead of using the default <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.AutoDetect>. |
| 31 | + |
| 32 | +## Rule description |
| 33 | + |
| 34 | +Starting with MSTest 3.8, `DynamicDataSourceType.AutoDetect` is the default value for the `DynamicDataSourceType` parameter in the `DynamicDataAttribute` constructor. This enhancement automatically detects whether the data source is a property, method, or field, eliminating the need to explicitly specify the source type. Using `AutoDetect` makes the code more maintainable and reduces verbosity. |
| 35 | + |
| 36 | +## How to fix violations |
| 37 | + |
| 38 | +Remove the explicit `DynamicDataSourceType` parameter from the `DynamicData` attribute and let the framework auto-detect the source type. |
| 39 | + |
| 40 | +For example, change this: |
| 41 | + |
| 42 | +```csharp |
| 43 | +public static IEnumerable<object[]> TestData { get; } = new[] |
| 44 | +{ |
| 45 | + new object[] { 1, 2, 3 }, |
| 46 | + new object[] { 4, 5, 9 } |
| 47 | +}; |
| 48 | + |
| 49 | +[TestMethod] |
| 50 | +[DynamicData(nameof(TestData), DynamicDataSourceType.Property)] |
| 51 | +public void TestMethod(int a, int b, int expected) |
| 52 | +{ |
| 53 | + Assert.AreEqual(expected, a + b); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +To this: |
| 58 | + |
| 59 | +```csharp |
| 60 | +public static IEnumerable<object[]> TestData { get; } = new[] |
| 61 | +{ |
| 62 | + new object[] { 1, 2, 3 }, |
| 63 | + new object[] { 4, 5, 9 } |
| 64 | +}; |
| 65 | + |
| 66 | +[TestMethod] |
| 67 | +[DynamicData(nameof(TestData))] |
| 68 | +public void TestMethod(int a, int b, int expected) |
| 69 | +{ |
| 70 | + Assert.AreEqual(expected, a + b); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## When to suppress warnings |
| 75 | + |
| 76 | +Don't suppress warnings from this rule. Following the analyzer suggestion leads to less noise in the test code. |
0 commit comments