Repository navigation
Description
Describe the bug
Please share a clear and concise description of the problem.
To Reproduce
I have the following class under Test
public partial class StuffMcStuffy
{
private readonly ILogger<StuffMcStuffy> _logger;
public StuffMcStuffy(ILogger<StuffMcStuffy> logger)
{
_logger = logger;
}
[LoggerMessage(
EventId = 0,
Level = LogLevel.Information,
EventName = "testname",
Message = "tes:extraction {JobId} {ExtractionStatus} {ExtractionError}")]
private partial void RecordExtractionEvent(Guid jobId, string extractionStatus, string extractionError);
[ExcludeFromCodeCoverage]
public void DoThis()
{
Console.Write("do this");
}
public void DoThat()
{
Console.Write("do that");
}
public void BlahThis()
{
var thislist = new List<KeyValuePair<string, object>>();
thislist.Add(new KeyValuePair<string, object>("hey", "ho"));
thislist.Add(new KeyValuePair<string, object>("hey1", "ho1"));
var blah = CreateLoggerDictionary(thislist);
blah.ToString();
}
[ExcludeFromCodeCoverage]
private static Dictionary<string, string> CreateLoggerDictionary(List<KeyValuePair<string, object>> attributes)
{
return attributes.ToDictionary(item => item.Key,
item => item.Value.ToString() ?? string.Empty);
}
}I used the following script to run the unit tests and generate the coverage reports
#!/usr/bin/env bash
set -euo pipefail
find . -type d -name TestResults -exec rm -rf {} +
rm -rf ./codeCoverageReport
PROJECTS="$(find . -type d -name "TestProject1")"
readonly PROJECTS
for project in ${PROJECTS}; do
dotnet test -c Release "$@" "${project}" --collect:"XPlat Code Coverage" --logger "console;verbosity=normal" --logger trx
done
dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
REPORT_TYPE=Html
COMMAND=open
FILENAME=index.html
TARGETDIRECTORY="$(ls -d "${dir}"/.)/codeCoverageReport"
REPORTS="$(find "${dir}/.." -type f -name "coverage.cobertura.xml" | tr "\n" ";")"
dotnet ~/.nuget/packages/reportgenerator/*/tools/*/ReportGenerator.dll \
-reports:"${REPORTS}" \
-targetdir:"${TARGETDIRECTORY}" \
-reporttypes:"${REPORT_TYPE}" && \
"${COMMAND}" "${TARGETDIRECTORY}/${FILENAME}"Expected behavior
The method CreateLoggerDictionary() should be excluded from test coverage
Actual behavior
The method is not excluded from coverage
** Workaround **
Rewrite the code using foreach()
private static Dictionary<string, string> CreateLoggerDictionary(List<KeyValuePair<string, object>> attributes)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (var attribute in attributes)
{
dictionary.Add(attribute.Key, attribute.Value.ToString() ?? string.Empty);
}
return dictionary;
}
Configuration (please complete the following information):
Please provide more information on your .NET configuration:
* Which coverlet package and version was used? v 6.0
* Which version of .NET is the code running on? .NET 6
* What OS and version, and what distro if applicable? Mac OS
* What is the architecture (x64, x86, ARM, ARM64)? ARM64
* Do you know whether it is specific to that configuration? No
Additional context
I've attached the project here
Coverlet-Test.zip
β Please also read Known Issues
Activity
[-][BUG][/-][+][BUG] ExcludeFromCodeCoverage[/+][-][BUG] ExcludeFromCodeCoverage[/-][+][BUG] ExcludeFromCodeCoverage does code exclude method in a partial class that contains a linq expression[/+]Kralizek commentedon Nov 7, 2023
I'm having the same issue
daveMueller commentedon Nov 8, 2023
Thansk for reporting and thanks a lot for the repro π. I just checked it and this will be fixed with PR #1542.
Kralizek commentedon Nov 9, 2023
That was fast! thanks @daveMueller
Bump coverlet.collector from 3.1.2 to 6.0.4 (#6)
chore(csharp): Bump coverlet.collector from 6.0.0 to 6.0.4 (#3314)
chore(csharp): Bump coverlet.collector from 6.0.0 to 6.0.4 (#3532)
Merge with ADBC 21 (#87)
Bump coverlet.console from 6.0.0 to 6.0.4 (#11)
Bump coverlet.collector from 6.0.0 to 6.0.4 (#105)
Bump coverlet.collector from 6.0.0 to 6.0.4 (#9)
Bump coverlet.collector from 6.0.0 to 6.0.4 (#9)
Bump coverlet.collector from 6.0.0 to 10.0.1 (#54)
Bump coverlet.collector from 6.0.0 to 10.0.1 (#46)
Bump coverlet.msbuild from 6.0.0 to 10.1.0 (#7768)