Skip to content

[BUG] Constructorless ControllerBase implementations produce no code coverage #1727

Description

@chrish619

Describe the bug
When producing code coverage for a WebApi/MVC project, if a Controller has no explicit constructor, it may not produce any code coverage results for the entire file.

These Controllers/Classes seem to be affected by the ExcludeByAttribute=CompilerGeneratedAttribute switch - as removing this option produces code coverage for these files.

To Reproduce
Sample Repository: https://github.com/chrish619/coverlet-coverage-issue-20250103/

Controller must inherit from either ControllerBase, or another constructor-less Controller abstraction.
ExcludeByAttribute in either MSBuild property flags, or a runsettings file, must contain CompilerGeneratedAttribute.

Expected behavior
WebApi/MVC Controllers without an explicit constructor, should produce code coverage results.

Actual behavior
WebApi/MVC Controllers without an explicit constructor produces no code coverage results when
ExcludeByAttribute=CompilerGeneratedAttribute is used.
These Controllers may use Actions with [FromServices] dependency injection - whilst also not declaring a constructor.

WebApi/MVC Controllers with an explicit constructor will always produce code coverage results.

Configuration (please complete the following information):
Please provide more information on your .NET configuration:

  • Which coverlet package and version was used? 6.0.2/6.0.3
  • Which version of .NET is the code running on? .Net8/.Net9
  • What OS and version, and what distro if applicable? Windows 10
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? Specifically caused by ExcludeByAttribute=CompilerGeneratedAttribute

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions