I found a couple of repositories with working unit tests:
All of them seem to be using “manual” approach with helper code included in the project. While they don’t shed any light on what’s going on with project templates and helper code bundled into “beta” nugets, at least they provide a working starting point.
UPDATE: Microsoft has updated its documentation; “Build your first analyzer and code fix” tutorial now has a note in Prerequisites section explaining that there’s a bug in the template that should be fixed in Visual Studio v16.7. The note also provides steps for fixing generated projects until then.
UPDATE 2: Followed the steps in the note. In fact, for test sources they are the same as provided by AndrewSilver in his answer. Got the same result, the second test fails. Oh well, back to square one.
UPDATE 3: Ok, I think I got it. Short version: add the following line to your analyzer’s
Spent some time debugging the library. The main culprit appeared to be this line that would declare your expected diagnostic message a subject to exclusion and discard it from the list of expected results, thus failing the test due to mismatch between actual (1) and expected (now 0) diagnostics. Was about to file an issue but decided to check if anything similar already exists. It does, indeed, and it points to another lengthy thread with this solution.
By the way, project’s README file provides a few more usage examples.
I’ve run into the same issue recently.
Thank you for mentioning changed nuget URL.
After changing it in VS settings, I’ve been able to compile project by removing
.CodeFix part in here
using Verify = Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest.CodeFixVerifier< Analyzer1.Analyzer1Analyzer, Analyzer1.Analyzer1CodeFixProvider>;
and after that by replacing
VerifyAnalyzerAsync in the first unit test and
VerifyCodeFixAsync in the second one.
Unfortunately, one of the unit tests is not working yet. I would appreciate any help with this issue.