-
Notifications
You must be signed in to change notification settings - Fork 71
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
Use Microsoft.CodeAnalysis.Testing for analyzer testing #122
Conversation
I will reopen this pull request after the prerequisite is merged. |
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="1.2.2" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 This version has a direct impact on downstream consumers. It should only be updated when necessary to use a new API, and even then alternatives should be used when possible.
5ae6d23
to
05bd2e5
Compare
@@ -10,6 +10,10 @@ | |||
<SignAssembly>true</SignAssembly> | |||
</PropertyGroup> | |||
|
|||
<ItemGroup> | |||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 This tool uses a newer version of the compiler to avoid warnings, but it's only a local tool so it doesn't have any impact on downstream consumers.
test/xunit.analyzers.tests/AssertCollectionContainsShouldNotUseBoolCheckTests.cs
Outdated
Show resolved
Hide resolved
@@ -21,165 +17,155 @@ public class AssertCollectionContainsShouldNotUseBoolCheckTests | |||
"System.Linq.Enumerable.Empty<int>()" | |||
}; | |||
|
|||
private static void CheckDiagnostics(IEnumerable<Diagnostic> diagnostics) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We appear to be losing a significant part of the testing here; namely, that we are validating the code, message, and severity of the check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are unlikely to change in a way that leads to a regression. With that said, there are a few things to keep in mind:
- Both the ID and the severity are getting tested (it asserts that they match the values provided in the diagnostic descriptor, which is not always a given).
- It is possible to write a more verbose test that covers any of these explicitly, and/or other situations that matter more for the library such as an assertion that no two analyzer types use the same ID.
- The new tests add dozens of new assertions covering cases that were not covered at all previously, including but not limited to the spans where the diagnostics are reported.
I'd recommend keeping most tests in this simplified form, and adding a minimal set of new tests to cover cases that might be prone to regression.
At the end of the day, I'm not sure I understand what this buys me. Can you tell me what's better about this? |
The new testing library automates the testing of a wide variety of tests relevant to analyzer authors. It's nothing that can't be done in other ways, but it plugs a bunch of test gaps compared to all the other approaches seen to date (by combining their various approaches). The tests themselves are deceptively simple, while providing high confidence in overall behavior. The shared library also helps analyzer authors contribute to each other's projects by offering a unified approach to testing. |
All development is moving to Linux and VS Code. The C# extension for VS Code only runs .NET Core unit tests. Since this PR disables .NET Core, I think I'm willing to wait until the library catches up to support .NET Core. |
3132adf
to
4c4efba
Compare
4c4efba
to
1b1ae30
Compare
As I was trying to clean up the source code, I ran across issues like this. This test: [Theory]
[MemberData(nameof(TypesAndValues))]
public async void FindsWarningForExpectedConstantOrLiteralValueAsNamedExpectedArgument(string type, string value)
{
var source =
@"class TestClass {
void TestMethod() {
var v = default(" + type + @");
Xunit.Assert.Equal(actual: " + value + @", expected: v);
}
}";
var expected = Verify.Diagnostic().WithLocation(3, 5).WithArguments(value, "Assert.Equal(expected, actual)", "TestMethod", "TestClass");
await Verify.VerifyAnalyzerAsync(source, expected);
} Failed with this message:
What is the purpose of including this in the assertion message? It doesn't seem to have any useful information in it, save perhaps the final line of the stack trace. |
@bradwilson I'm not sure I understand the question. There are some cases remaining where the internal assertions of the testing library might not make sense to users. We've been fixing those cases as they are found, with the goal of making the failures messages lead directly to the expected solutions. I'm not seeing that test fail locally so I'm not sure what the specific situation here is. |
@sharwell I'm questioning the value of your assertion messages in your test library when there is a failure. Your assertion message starts out with useful information (like 'expected diagnostic to start on line 3, but started on line 4'), but then some internal xUnit.net assertion is appended to this message (like the one shown above) which is both unnecessary and confusing. Furthermore, the stack trace includes a bunch of unnecessary lines from your testing framework. This:
Would be more useful/less noisy if it looked like this:
|
5fd23d7
to
d0da7b1
Compare
I would not be opposed to improving the presentation following a failure in this group of assertions. It's one of the more clumsy sets of output. For example, instead of comparing "2" and "3", it might be better to present the comparison as: Diagnostic was not at the correct location. Showing expected as baseline:
-Test0.cs(3,9)
+Test0.cs(2,9) This could use some more brainstorming. Please consider filing an issue for this in dotnet/roslyn-sdk. |
There are some line-ending issues still remaining with non-Windows (Roslyn code fixers will unilaterally use |
As for the issue with .NET Framework and Visual Studio Test Explorer, I'm comfortable moving forward without a resolution because (a) I did not open an issue with the VSTest team. I suggest that you do so, since it appears to be at least somewhat related to dependency resolution with your test framework rather than xUnit.net itself that is at issue here. |
I'm trying to fix up the Linux issues, but non-AppDomains now appears to be broken:
As does AppDomains with shadow copy turned off:
I think I'm going to have to revert this for now until these issues can be resolved. |
This reverts commit 2c05a98.
I'm planning to split the fixer tests into their own assembly, so I'll apply your rollbacks to the .csproj files at the same time. |
(This should be a simple rename in git, so your changes to those files should be able to track across the rename.) |
@bradwilson Can you send me a message on Teams? |
This test library provides a more comprehensive set of test features for analyzer authors. The primary barrier to adoption is the initial conversion, which I've done here for the full xunit.analyzers suite.