Skip to content

MSTEST0025: Use 'Assert.Fail' instead of an always-failing assert #40785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/core/testing/mstest-analyzers/mstest0025.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "MSTEST0025: Use 'Assert.Fail' instead of an always-failing assert"
description: "Learn about code analysis rule MSTEST0025: Use 'Assert.Fail' instead of an always-failing assert"
ms.date: 05/07/2024
f1_keywords:
- MSTEST0025
- PreferAssertFailOverAlwaysFalseConditionsAnalyzer
helpviewer_keywords:
- PreferAssertFailOverAlwaysFalseConditionsAnalyzer
- MSTEST0025
author: Evangelink
ms.author: amauryleve
---
# MSTEST0025: Use 'Assert.Fail' instead of an always-failing assert

| Property | Value |
|-------------------------------------|-------------------------------------------------------|
| **Rule ID** | MSTEST0025 |
| **Title** | Use 'Assert.Fail' instead of an always-failing assert |
| **Category** | Design |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default** | Yes |
| **Default severity** | Info |
| **Introduced in version** | 3.4.0 |

## Cause

This rule raises a diagnostic when a call to an assertion produces an always-false condition.

## Rule description

Using `Assert.Fail` over an always-failing assertion call provides clearer intent and better documentation for the code.

When you encounter an assertion that always fails (for example, `Assert.IsTrue(false)`), it might not be immediately obvious to someone reading the code why the assertion is there or what condition it's trying to check. This can lead to confusion and wasted time for developers who come across the code later on.

In contrast, using `Assert.Fail` allows you to provide a custom failure message, making it clear why the assertion is failing and what specific condition or scenario it's addressing. This message serves as documentation for the intent behind the assertion, helping other developers understand the purpose of the assertion without needing to dive deep into the code.

Overall, using `Assert.Fail` promotes clarity, documentation, and maintainability in your codebase, making it a better choice over an always failing assertion call.

## How to fix violations

Ensure that calls to `Assert.IsTrue`, `Assert.IsFalse`, `Assert.AreEqual`, `Assert.AreNotEqual` or `Assert.IsNotNull` are not producing always-failing conditions.

## When to suppress warnings

We do not recommend suppressing warnings from this rule.