# C# Exception Code Coverage in Azure DevOps

**URL:** <https://community.sonarsource.com/t/c-exception-code-coverage-in-azure-devops/30474>\
**Category:** SonarQube Cloud\
**Tags:** azuredevops-services, csharp, coverage, scanner\
**Created:** [September 2, 2020, 8:52am UTC](https://community.sonarsource.com/t/c-exception-code-coverage-in-azure-devops/30474 "2020-09-02T08:52:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alext](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alext/32/16679_2.png) [@alext](https://community.sonarsource.com/u/alext)\
**Post date:** [September 2, 2020, 8:52am UTC](https://community.sonarsource.com/t/c-exception-code-coverage-in-azure-devops/30474/1 "2020-09-02T08:52:40Z")

</div>

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

 ![sonar1](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/3/368d8101bb7df630b64b609f89e447ae7c44bbcf.png)

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:  
 ![sonar2](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/c/c08d16a6f275daa3d2ec1e5403c93d206255c106.png)

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!

---

<div class="post-metadata">

**Author:** ![Andrei\_Epure](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andrei_epure/32/77402_2.png) [@Andrei\_Epure](https://community.sonarsource.com/u/Andrei_Epure)\
**Post date:** [September 3, 2020, 2:54pm UTC](https://community.sonarsource.com/t/c-exception-code-coverage-in-azure-devops/30474/2 "2020-09-03T14:54:15Z")

</div>

hi @alext and welcome to this community!

would it be possible to come up with a minimal reproducible project and send it, together with the code coverage file for it, so we can reproduce it locally and investigate the problem?

Also, please let us know more details about the environment:

- What visual studio coverage tool version are you using?
- what is the version of Scanner for MSBuild you are using?
- what is the version of MSBuild you are using?

> [@alext](#):
>
> The Prepare Analysis Configuration task is configured as follows, excluding certain files from analysis:
> 
> ```auto
> # 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
> 
> ```

I imagine you also pass the `sonar.cs.vscoveragexml.reportsPaths` property to the command? See [[Coverage & Test Data] Generate Reports for C#, VB.net](https://community.sonarsource.com/t/coverage-test-data-generate-reports-for-c-vb-net/9871) .

---

<div class="post-metadata">

**Author:** ![alext](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alext/32/16679_2.png) [@alext](https://community.sonarsource.com/u/alext)\
**Post date:** [September 8, 2020, 12:16pm UTC](https://community.sonarsource.com/t/c-exception-code-coverage-in-azure-devops/30474/3 "2020-09-08T12:16:52Z")

</div>

Hi @Andrei_Epure

Thanks for the reply. Upon investigation, it was not an issue with the exceptions themselves but a configuration issue on our end.

We have two test projects, Tests.Unit and Tests.Integration, and the pipeline was only providing the trx and coverage files for the Tests.Integration project, which is why the coverage was not as expected.

This has now been resolved. Thank you for your support in any case.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [September 15, 2020, 12:17pm UTC](https://community.sonarsource.com/t/c-exception-code-coverage-in-azure-devops/30474/4 "2020-09-15T12:17:01Z")

</div>

Closed on September 15, 2020, 12:17pm UTC

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