How to get code coverage for .Net project using Github Actions and provided worfkflow template

I am looking for examples of workflows based on the ready template provided for .Net projects, that successfully run unit tests and have the code coverage displayed in SonarCloud, or simply directions on how I can set it up for my workflow and project

Hey there.

Based on your logs, it doesn’t look like tests actually get run

2021-12-07T12:49:25.4988087Z Time Elapsed 00:01:51.52
2021-12-07T12:49:25.5128470Z trying
2021-12-07T12:49:25.5130652Z to
2021-12-07T12:49:25.5132357Z run
2021-12-07T12:49:25.5142777Z tests

It looks like you have tests running properly in your other workflows (curious why you’ve split out the SonarCloud analysis from the workflow where you already have a successful build/test), and would want to mimic what you’re doing there to get your tests running.

The sonarcloud workflow uses a different runner, so this is the reason it’s split up for now. Still early days in the projects, so trying to get all the parts to work before we figure out what the final structure and optimization of workflows will be.

The workflow for analysis is simply copied from the template provided when I try to configure the analysis method, so I would hope that it would work out of the box, just by plugging in the run & test cmd

@Colin do you have any examples of workflows available that have used the code snipped provided during setup of .NET project as their base?

@acn-sbuad I don’t think this is a case of things not working out of the box, I think you’ve just supplied the wrong test command.

I forked your project and by changing your multi-line dotnet test command to a simple dotnet test the tests clearly ran. So I don’t think you’re far off :smiley:

I really suggest focusing on how you’ve constructed your test command with the various options you added and making sure it actually executes tests when you execute them in a local environment.

Specifically, I’d try taking out --no-build and --no-restore from your 'dotnet test` command. You can try out this command yourself in your local environment… these options simply make nothing happen… no tests ran = no coverage report produced.

Once you have tests run and a coverage report produced, you’ll need to make sure to pass the generated report to the proper property for opencover. What this means in practice is adding /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" to the end of your dotnet-sonarscanner begin command.

And then you should be in good shape!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.