|
| 1 | +--- |
| 2 | +title: "Use proper 'Assert' methods" |
| 3 | +description: "Learn about code analysis rule MSTEST0037: Use proper 'Assert' methods." |
| 4 | +ms.date: 11/17/2024 |
| 5 | +f1_keywords: |
| 6 | +- MSTEST0037 |
| 7 | +- UseProperAssertMethodsAnalyzer |
| 8 | +helpviewer_keywords: |
| 9 | +- UseProperAssertMethodsAnalyzer |
| 10 | +- MSTEST0037 |
| 11 | +author: Youssef1313 |
| 12 | +ms.author: ygerges |
| 13 | +--- |
| 14 | +# MSTEST0037: Use proper 'Assert' methods |
| 15 | + |
| 16 | +| Property | Value | |
| 17 | +|-------------------------------------|------------------------------------------------------------------------| |
| 18 | +| **Rule ID** | MSTEST0037 | |
| 19 | +| **Title** | Use proper 'Assert' methods | |
| 20 | +| **Category** | Usage | |
| 21 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 22 | +| **Enabled by default** | Yes | |
| 23 | +| **Default severity** | Info | |
| 24 | +| **Introduced in version** | 3.7.0 | |
| 25 | +| **There is a code fix** | No | |
| 26 | + |
| 27 | +## Cause |
| 28 | + |
| 29 | +The use of <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> methods in a specific way when there is a better alternative. |
| 30 | + |
| 31 | +## Rule description |
| 32 | + |
| 33 | +There are multiple cases where you get this warning: |
| 34 | + |
| 35 | +- The use of `Assert.IsTrue(<expression> == null)` (with all combinations, like `IsFalse`, `!= null`, `is null`, or `is not null`). |
| 36 | + |
| 37 | + Using `Assert.IsNull(<expression>)` or `Assert.IsNotNull(<expression>)` is a better alternative. |
| 38 | + |
| 39 | +- The use of `Assert.IsTrue(<expression1> == <expression2>)` (with all combinations, like `IsFalse` or `!=`). |
| 40 | + |
| 41 | + Using `Assert.AreEqual(<expression1>, <expression2>)` or `Assert.AreNotEqual(<expression1>, <expression2>)` is a better alternative. |
| 42 | + |
| 43 | +- The use of `Assert.AreEqual(true, <expression>)` or `Assert.AreEqual(false, <expression>)`. |
| 44 | + |
| 45 | + Using `Assert.IsTrue(<expression>)` or `Assert.IsFalse(<expression>)` is a better alternative. |
| 46 | + |
| 47 | +- The use of `Assert.AreEqual(null, <expression>)` or `Assert.AreNotEqual(null, <expression>)`. |
| 48 | + |
| 49 | + Using `Assert.IsNull(<expression>)` or `Assert.IsNotNull<expression>` is a better alternative. |
| 50 | + |
| 51 | +In many cases, the better alternatives provide better messages when they fail and are also easier to read. |
| 52 | + |
| 53 | +## How to fix violations |
| 54 | + |
| 55 | +Use the better alternative method. |
| 56 | + |
| 57 | +## When to suppress warnings |
| 58 | + |
| 59 | +You usually don't want to suppress warnings from this rule. |
0 commit comments