Skip to content

Using PublicAPI analyzer in unit tests #5889

Open

Description

Right now, the PublicAPI analyzer is integrated into the project that is going to be monitored by the analyzer.

I have a library for which I would like to "freeze" the API and I would create a unit test project that tests whether the monitored
project has breaking changes relative to the baseline API.

I would like to know if it's possible to consume the PublicAPI analyzer as an actual reference in a test project, and call methods of the analyzer to manually generate the API of another project, so I can diff it with a previously stored one, something like this:

[Test]
void TestApiBreakingChanges()
{
     string shippedAPI = System.IO.File.ReadAllText("BaselineAPI.txt");
     string UnshippedAPI = Microsoft.CodeAnalysis.PublicApiAnalyzers.CreateApiFromProject("..\SomeProject\SomeProject.csproj");

     Assert.AreEqual(shippedAPI , UnshippedAPI);
}

Is this case scenario supported?

Additional context

This way of monitoring the API changes has a number of advantages:

  • It gives developers a programatic way of testing when and what is considered a breaking change.
  • It gives fine grain control over the API changes, so you can do specific tests over a subset of the API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions