-
Notifications
You must be signed in to change notification settings - Fork 261
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
new Analyzer+CodeFix: recommend against wrong assertion method (e.g. Assert.IsTrue(x != null)
)
#3315
Comments
Assert.IsTrue(x != null)
)Assert.IsTrue(x != null)
)
Even though it's more tedious than complex, I was given permission to share our existing code for this Analyzer and CodeFix if necessary, since it only affects MsTest. |
Hi @LukasGelke, I have a good idea of how to implement this analyzer but if you are allowed to share the code, please feel free to paste it/enclose it here. If we haven't yet started by end of August, please feel free to do the contribution :) Thanks again! |
Hey @Evangelink, I'm sorry. But i just got around to start working on this. I've successfully install net9 (from the "plain" installer) though; as complained at me by the C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(12
59,5): error MSB3644: The reference assemblies for .NETCore,Version=v5.0 were not found. To resolve this, install the D
eveloper Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Frame
work Developer Packs at https://aka.ms/msbuild/developerpacks [D:\GitHub\testfx\src\Adapter\MSTestAdapter.PlatformServi
ces\MSTestAdapter.PlatformServices.csproj::TargetFramework=uap10.0.16299]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(12
59,5): error MSB3644: The reference assemblies for .NETCore,Version=v5.0 were not found. To resolve this, install the D
eveloper Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Frame
work Developer Packs at https://aka.ms/msbuild/developerpacks [D:\GitHub\testfx\src\Adapter\MSTest.TestAdapter\MSTest.T
estAdapter.csproj::TargetFramework=uap10.0.16299]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(12
59,5): error MSB3644: The reference assemblies for .NETCore,Version=v5.0 were not found. To resolve this, install the D
eveloper Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Frame
work Developer Packs at https://aka.ms/msbuild/developerpacks [D:\GitHub\testfx\src\TestFramework\TestFramework.Extensi
ons\TestFramework.Extensions.csproj::TargetFramework=uap10.0.16299] > dotnet --list-sdks
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.425 [C:\Program Files\dotnet\sdk]
7.0.102 [C:\Program Files\dotnet\sdk]
7.0.120 [C:\Program Files\dotnet\sdk]
8.0.108 [C:\Program Files\dotnet\sdk]
8.0.206 [C:\Program Files\dotnet\sdk]
8.0.400 [C:\Program Files\dotnet\sdk]
9.0.100-rc.1.24452.12 [C:\Program Files\dotnet\sdk]
> dotnet --list-runtimes
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-rc.1.24452.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-rc.1.24431.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0-rc.1.24452.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] |
|
The analyzer is now implemented. The issue is open to track implementing codefix |
Summary
As some assertions may be written in different ways, some of them provide a better experience: better/richer message, clearer intent, better readability/maintainability.
Background and Motivation
This is also useful when a test is failing in a pipeline, or you get the stack trace only. This way you may not have to dig too deep and "waste" time debugging, just to get to that point to know what
text
actually is. Or when you get a notification from the pipeline a latest method "VeryImportantTest" failed with message "Assert.IsTrue failed."
#pragma
or Attribute)Proposed Feature
essentially all permutations of IsFalse/IsTrue with
is
/is not
/==
/!=
Assert.IsNotNull, e.g.
Assert.IsTrue(x != null)
=>Assert.IsNotNull(x)
Assert.IsTrue(null != x)
=>Assert.IsNotNull(x)
Assert.IsFalse(x != null)
=>Assert.IsNull(x)
Assert.IsTrue(x is not null)
=>Assert.IsNotNull(x)
Assert.AreNotEqual(null, x)
=>Assert.IsNotNull(x)
null
literalAssert.Are[Not]Equal, e.g.
Assert.IsTrue(x == y)
=>Assert.AreEqual(x, y)
Assert.IsTrue(x != y)
=>Assert.AreNotEqual(x, y)
Assert.IsFalse(x == y)
=>Assert.AreNotEqual(x, y)
Assert.IsFalse(x != y)
=>Assert.AreEqual(x, y)
expected
' be handled by the other analyzerAssert.IsTrue/IsFalse, e.g.
Assert.AreEqual(true, b)
=>Assert.IsTrue(b)
Assert.IsTrue(true == b)
, stupid, but possible; again, just let it do one step at a time, for simplicity?Edit 1:
maybe also consider:
Assert.AreEqual(typeof(string), o.GetType())
=>Assert.IsInstanceOfType<string>(o)
also wouldn't matter to me if they are all the same DiagnosticID, since those cases are all "wrong similarly"
Alternative Designs
As mentioned, our analyzer grew over time and is quite complex. And I'm not sure if we have a bug or a missing case. Having such an Analyzer+CodeFix in MsTest directly would also provide a wider audience for finding such "bugs".
AB#2200926
The text was updated successfully, but these errors were encountered: