Repository navigation
Description
dotnet version: 10.0.404
dotnet-coverage version: 18.12.0+e67518936dc54d459b37375c5eef5dc89f05d535
Given I have code that is only executed when a precompiler symbol is defined (DEBUG in this case):
namespace DummyLib
{
public class Class
{
public HttpClientHandler httpClientHandler;
public Class()
{
httpClientHandler = new HttpClientHandler
{
#if DEBUG
ServerCertificateCustomValidationCallback = (_, _, _, _) => { return true; }
#endif
};
}
}
}When I write a test covering this conditional code:
using DummyLib;
namespace DummyTest
{
[TestClass]
public sealed class ClassTest
{
[TestMethod]
public void Ctor_DoesNothing()
{
var sut = new Class();
Assert.IsNotNull(sut
.httpClientHandler
.ServerCertificateCustomValidationCallback);
Assert.IsNotNull(sut
.httpClientHandler
.ServerCertificateCustomValidationCallback?
.Method.DeclaringType);
var decl = sut
.httpClientHandler
.ServerCertificateCustomValidationCallback
.Method
.DeclaringType
.DeclaringType;
Assert.IsTrue(decl == typeof(Class));
}
}
}And When the test returns with the result of having succeeded, therefore suggesting the conditional line got executed:
PS D:\Repos\cs-sandbox> dotnet test
Running tests from DummyTest\bin\Debug\net10.0\DummyTest.dll (net10.0|x64)
DummyTest\bin\Debug\net10.0\DummyTest.dll (net10.0|x64) passed (630ms)
Test run summary: Passed!
total: 1
failed: 0
succeeded: 1
skipped: 0
duration: 1s 010ms
PS D:\Repos\cs-sandbox>
And When I comment-out the conditionally executed assignment:
...
#if DEBUG
// ServerCertificateCustomValidationCallback = (_, _, _, _) => { return true; }
#endif
...And When in this case the test returns with a result of having failed, therefore proving the conditional line gets executed:
PS D:\Repos\cs-sandbox> dotnet test
Running tests from DummyTest\bin\Debug\net10.0\DummyTest.dll (net10.0|x64)
failed Ctor_DoesNothing (28ms)
Assert.IsNotNull failed. 'value' expression: 'sut
.httpClientHandler
.ServerCertificateCustomValidationCallback'.
from DummyTest\bin\Debug\net10.0\DummyTest.dll (net10.0|x64)
Assert.IsNotNull failed. 'value' expression: 'sut
.httpClientHandler
.ServerCertificateCustomValidationCallback'.
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowAssertIsNotNullFailed(String message) in /_/src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs:202
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull(Object value, String message, String valueExpression) in /_/src/TestFramework/TestFramework/Assertions/Assert.IsNull.cs:194
at DummyTest.ClassTest.Ctor_DoesNothing() in DummyTest\ClassTest.cs:13
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
DummyTest\bin\Debug\net10.0\DummyTest.dll (net10.0|x64) failed with 1 error(s) (684ms)
Exit code: 2
Standard output: MSTest v4.0.2 (UTC 2025-11-11) [win-x64 - .NET 10.0.15]
Test Parallelization enabled for D:\Repos\cs-sandbox\DummyTest\bin\Debug\net10.0\DummyTest.dll (Workers: 28, Scope: MethodLevel)
Test run summary: Failed!
total: 1
failed: 1
succeeded: 0
skipped: 0
duration: 1s 034ms
Test run completed with non-success exit code: 2 (see: https://aka.ms/testingplatform/exitcodes)
PS D:\Repos\cs-sandbox>
And When I define code coverage settings as follows:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<CodeCoverage>
<ModulePaths>
<Include>
<ModulePath>.*DummyLib\.dll$</ModulePath>
</Include>
</ModulePaths>
</CodeCoverage>
</Configuration>And When I collect the coverage for the abovementioned code:
dotnet-coverage collect dotnet test -f cobertura -o coverage.xml -s .\CodeCoverage.runsettings --verbosity detailed
Then I end up with a coverage report marking the conditionally executed line as uncovered:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<coverage line-rate="0.8888888888888888" branch-rate="1" complexity="3" version="1.9" timestamp="1795184469" lines-covered="8" lines-valid="9" branches-covered="2" branches-valid="2">
<packages>
<package line-rate="0.8888888888888888" branch-rate="1" complexity="3" name="DummyLib">
<classes>
<class line-rate="1" branch-rate="1" complexity="2" name="DummyLib.Class" filename="D:\Repos\cs-sandbox\DummyLib\Class.cs">
<methods>
<method line-rate="1" branch-rate="1" complexity="2" name=".ctor" signature="()">
<lines>
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="10" hits="1" branch="False" />
<line number="11" hits="1" branch="False" />
<!-- mark the missing line number 12 here -->
<line number="13" hits="1" branch="False" />
<line number="14" hits="1" branch="False" />
<line number="15" hits="1" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="7" hits="1" branch="False" />
<line number="8" hits="1" branch="False" />
<line number="9" hits="1" branch="True" condition-coverage="100% (2/2)">
<conditions>
<condition number="0" type="jump" coverage="100%" />
</conditions>
</line>
<line number="10" hits="1" branch="False" />
<line number="11" hits="1" branch="False" />
<!-- mark the missing line number 12 here -->
<line number="13" hits="1" branch="False" />
<line number="14" hits="1" branch="False" />
<line number="15" hits="1" branch="False" />
</lines>
</class>
<class line-rate="0" branch-rate="1" complexity="1" name="DummyLib.Class.<>c" filename="D:\Repos\cs-sandbox\DummyLib\Class.cs">
<methods>
<method line-rate="0" branch-rate="1" complexity="1" name="<.ctor>b__1_0" signature="(System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2, System.Security.Cryptography.X509Certificates.X509Chain, System.Net.Security.SslPolicyErrors)">
<lines>
<line number="12" hits="0" branch="False" />
</lines>
</method>
</methods>
<lines>
<line number="12" hits="0" branch="False" />
</lines>
</class>
</classes>
</package>
</packages>
</coverage>And I think that's a false-negative.
Activity
ThomasMeschke commentedon Nov 20, 2026
Project to re-create the issue:
cs-sandbox.zip