microsoft / testfx

MSTest framework and adapter
MIT License
750 stars 254 forks source link

new Analyzer+CodeFix: recommend against wrong assertion method (e.g. `Assert.IsTrue(x != null)`) #3315

Open LukasGelke opened 3 months ago

LukasGelke commented 3 months ago

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

Proposed Feature

essentially all permutations of IsFalse/IsTrue with is/is not/==/!=

Edit 1: maybe also consider:

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

LukasGelke commented 3 months ago

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. I may also be able to contribute it directly, if I could get the build to work. However due to current planning, this is only possible on 2014-08-26 at the earliest. (and as per guide, would only start if this is approved and I'm allowed to do so)

Evangelink commented 3 months ago

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!

LukasGelke commented 1 month ago

Hey @Evangelink, I'm sorry. But i just got around to start working on this. All seems good, except one thing: .\Build.cmd and VS (.\MSTest.slnf) still complain that net5 is somehow missing. Also the dev pack for net5 is not available for VS2022, so I'm not sure what to install, except the "plain" SDK. The docs/dev-guide.md seems a bit out-of-date since Universal Windows Platform development and .Net Core cross-platform development are available as "workloads" anymore(?). I do however have the "individual component" Windows 10 SDK (10.0.20348.0) installed already. (My machine is still Win10)

I've successfully install net9 (from the "plain" installer) though; as complained at me by the global.json which now seems to work.

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]