Skip to content

Commit 18ecefa

Browse files
authored
Document MSTEST0037 analyzer (#43601)
1 parent 3208829 commit 18ecefa

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.

docs/core/testing/mstest-analyzers/usage-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Identifier | Name | Description
2727
[MSTEST0023](mstest0023.md) | DoNotNegateBooleanAssertionAnalyzer | Do not negate boolean assertions
2828
[MSTEST0024](mstest0024.md) | DoNotStoreStaticTestContextAnalyzer | Do not store TestContext in a static member
2929
[MSTEST0026](mstest0026.md) | AssertionArgsShouldAvoidConditionalAccessRuleId | Avoid conditional access in assertions
30+
[MSTEST0037](mstest0037.md) | UseProperAssertMethodsAnalyzer | Use proper `Assert` methods

docs/navigate/devops-testing/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ items:
187187
href: ../../core/testing/mstest-analyzers/mstest0034.md
188188
- name: MSTEST0035
189189
href: ../../core/testing/mstest-analyzers/mstest0035.md
190+
- name: MSTEST0037
191+
href: ../../core/testing/mstest-analyzers/mstest0037.md
190192
- name: Microsoft Testing Platform
191193
items:
192194
- name: Overview

0 commit comments

Comments
 (0)