We recently started to use VerifyTests/Verify on Mstest .Net 8. Projects.
One of my colleagues has an error on local build:
CS0119 ’TestContext’ is a type, which is not valid in the given
context
when accessing the TestContext property in a class like
[TestClass]
[UsesVerify]
public partial class ItineraryAttachmentTests
{
private async Task<string?> Test( )
{
return await DebugSaveAndViewModelAsHtml( TestContext);
}
}
The error doesn’t happen on other developers machines.
On the GitHub Actions build server the normal build/tests run correctly.
But CodeQL analyser reports the same error:
We were unable to automatically build your code. Please replace the call to the autobuild action with your custom build steps. Encountered a fatal error while running “/opt/hostedtoolcache/CodeQL/2.20.0/x64/codeql/csharp/tools/autobuild.sh”. Exit code was 1 and last log line was: Error: Could not auto-detect a suitable build method. See the logs for more details.
error CS0119: ‘TestContext’ is a type, which is not valid in the given context
The build compiler correctly understands that ‘TestContext’ is a property , implemented in auto-generated partial class , but the analyser doesn’t see the generated class .
Do you have any suggestions, how to avoid the error?
How can we somehow troubleshoot or enforce src/Verify.MSTest.SourceGenerator, if it doesn’t work?
The functionality of the SourceGenerator described in Use attribute instead of base class for MSTest #1193
It is expected that file like the following will be generated:
C:UsersMichael.FreidgeimAppDataLocalTempVSGeneratedDocumentscc80ecff-236e-87d6-92d8-0d293fe16153UsesVerify.g.cs
namespace UnitTests
{
partial class ItineraryAttachmentTests
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Verify.MSTest.SourceGenerator", "1.0.0.0")]
public global::Microsoft.VisualStudio.TestTools.UnitTesting.TestContext TestContext
{
get => global::VerifyMSTest.Verifier.CurrentTestContext.Value!.TestContext;
set => global::VerifyMSTest.Verifier.CurrentTestContext.Value = new global::VerifyMSTest.TestExecutionContext(value, GetType());
}
}
}
I’ve noticed the warning, that explains the issue
CSC : warning CS9057: The analyzer assembly
‘/home/runner/.nuget/packages/verify.mstest/28.2.0/analyzers/dotnet/roslyn4.4/cs/Verify.MSTest.SourceGenerator.dll’ references version ‘4.9.0.0’ of the compiler, which is newer than the
currently running version ‘4.8.0.0’.
We will need to find, why the older version of Roslyn is called?