Skip to main content

Solve code coverage issue with Azure Functions

 I tried to execute code coverage for an Azure Function project with the Coverlet NuGet package. When running the unit test phase I received the following error message and no code coverage file was generated for the Azure Function project.

Data collector 'XPlat code coverage' message: [coverlet]System.TypeLoadException: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.GetDefaultServiceCollection(TestPlatformEqtTrace eqtTrace, TestPlatformLogger logger, String testModule)

   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionStart(Object sender, SessionStartEventArgs sessionStartEventArgs) in /_/src/coverlet.collector/DataCollection/CoverletCoverageCollector.cs:line 135.

To solve this issue you have to add the Microsoft.Extensions.DependencyInjection.Abstractions NuGet package to the unit test project which references the Azure Function project. When this reference is added the code coverage file will be generated.

Comments