Hi,
We are using SonarCloud for code analysis in Azure DevOps on a C# .NET Core application.
There is a recurring issue with lines throwing exceptions showing as uncovered, as shown below
The total code coverage for this file is shown as 36.8%, with all of the thrown exceptions being highlighted as uncovered.
However, these exceptions are being explicitly tested in a test file using xUnit:
[Fact]
public void It_should_not_allow_empty_importance_value()
{
try
{
_validator.ValidateImportance("", 0);
}
catch (FileFormatException ex)
{
Assert.Equal("Missing importance value in row 1", ex.Message);
}
}
the ‘Assert.Throws’ assertion was also used:
[Fact]
public void It_should_not_allow_empty_importance_value()
{
Assert.Throws<FileFormatException>(() => _validator.ValidateImportance("", 0));
}
Inspecting the .coverage file generated by the build pipeline showed this file as 100% covered:
The Prepare Analysis Configuration task is configured as follows, excluding certain files from analysis:
# Additional properties that will be passed to the scanner,
# Put one key=value per line, example:
sonar.exclusions=**/*.bin,**/WebApi/Migrations/*,**Tests.Unit/*,**Tests.Integration/*,**/WebApi/Extensions/EfCoreExtensions.cs
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/TestResults/*.trx
Running the tests locally returns 100% coverage for the given file.
Is there an issue with C# code coverage of exceptions? This has been an issue throughout the project where thrown exceptions are shown as not being covered.
Please let me know if any additional information is needed. Any suggestions are appreciated. Thanks!