-
Notifications
You must be signed in to change notification settings - Fork 256
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
Need documentation/examples for setting references used by tests #597
Comments
If all of your tests need to add this reference, you can change the default Lines 10 to 15 in 4baf9ef
Lines 12 to 14 in 4baf9ef
If you only need to run a single test with a different set of reference assemblies, the recommended approach is to switch from await new VerifyVB.Test
{
ReferenceAssemblies = referenceAssemblies, // <-- set this to the appropriate set of assemblies
TestState =
{
Sources = { test },
ExpectedDiagnostics = { VerifyVB.Diagnostic().WithLocation(0).WithArguments("TypeName") },
},
}.RunAsync(); |
Thanks, it would be great if this was documented somewhere easier to find or even in the comments. |
This is really a problem. The comments on this thread are not enough to really help. Running code analysis tests on sample code with external dependencies doesn't seem like it should be so hard to do. The guidance here is "set reference assemblies to the appropriate set of assemblies". What does that actually mean? Where do the referenced assemblies have to be located? What do you do in the case of a nuget package? |
@sharwell What's the difference between |
Here's my setup: public sealed class ResultNotDiscardedAnalyzerTests
{
private const string Code = @"
using Utilites;
class Foo
{
void Bar() { Result.Ok(); }
}
";
[Fact]
public async Task Test()
{
await new CSharpAnalyzerTest<ResultNotDiscardedAnalyzer, XUnitVerifier>
{
ReferenceAssemblies = ReferenceAssemblies.Default.WithAssemblies(
ImmutableArray.Create(typeof(Result).Assembly.Location)),
TestState =
{
Sources = { Code }
}
}.RunAsync();
}
} Test fails with:
|
This one wroks: [Fact]
public async Task Test()
{
await new CSharpAnalyzerTest<ResultNotDiscardedAnalyzer, XUnitVerifier>
{
ReferenceAssemblies = new ReferenceAssemblies(
"net6.0", new PackageIdentity("Microsoft.NETCore.App.Ref", "6.0.0"), Path.Combine("ref", "net6.0")),
TestState =
{
Sources = { Code },
AdditionalReferences = { typeof(Result).Assembly.Location }
}
}.RunAsync();
} Yet I have no idea, why. |
|
@sharwell, thanks for pointing out that
In my case I was lucky to be able to change the project to |
@MihailsKuzmins Use |
Is there a way to add all assembly dependencies to |
@sergiojrdotnet The only way I would recommend is using the Here's an example of adding the System.ValueTuple package for a test against net46: Lines 349 to 357 in f9932c8
|
Here's an example where we needed to test against some ASP.NET packages: |
Great! Thank you @sharwell |
@sharwell where do you get new PackageIdentity("System.ValueTuple", "4.5.0")? I was looking for WinForms where do I get its PackageIdentity for .Net 7? |
@paul1956 You should be able to use the same packages that appear in a real project targeting WinForms on .NET 7. |
@sharwell that is all a black box is there a file I can look into for the full string? All I specify in project file is below
|
|
I'm running in to issues with the reference assemblies. The problem we have is that it tries to resolve nuget packages from the feed, but because we have a private feed in azure devops it failes with a 401. It does not seem to use existing tokens that visual studio or dotnet has, like a normal unit test would have. Why can't we just reference packages in the unit test normally and have the analyzer test use these? The whole analyzer debugging/testing experience is extremely poor and not well documented. |
These would not accurately represent builds. Some NuGet packages have different assemblies in the In addition, unit tests in a single project can test against different target frameworks, e.g. I'm not aware of a solution for using authenticated feeds in unit tests, unless it's possible to do so through this feature. It might be necessary to manually construct
Yes, this project is known to have a somewhat steep learning curve. It wasn't intentional, but available resources are constrained in this space. |
Update to remove duplicate issues with #598
It is not obvious how you add a reference to Roslyn into tests. Changing VerifyAnalyzerAsync is hard to discover and doesn't seem to work.
With the test
You get errors
The text was updated successfully, but these errors were encountered: