我轻松配置为获取 Azure DevOps 中项目的覆盖率结果.NET Core
,但项目的运气不佳.NET Framework
。
因此,我将非常感激获得有关此问题的建议,因为封面文档明确表示我们也可以将其用于.NET Framework
项目。这个问题与我的有点相似,但我在那里没有看到任何答案,
你可以使用 Coverlet 获取 .NET Framework 项目中的代码覆盖率数据吗?
我正在 Linux 机器内使用 VS 代码开发 dotnet core 项目。
按照以下博客生成覆盖率报告: https://www.hanselman.com/blog/AutomaticUnitTestingInNETCorePlusCodeCoverageInVisualStudioCode.aspx
正如那里提到的,我传递了 dotnet 测试参数,如下所示:
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info
Run Code Online (Sandbox Code Playgroud)
并期望生成 lcov.info 文件,但它没有。
我在这里错过了什么吗?
ps:我已经包含这个包了
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Run Code Online (Sandbox Code Playgroud) 我编写了一个非常简单的 C# .Net Framework 应用程序并添加了一些单元测试。现在,尝试使用床单检查覆盖范围。出现错误 MSB4184。
无法计算表达式“[System.Version]::Parse('')”。版本字符串部分太短或太长。(参数“输入”)。
有人遇到这样的错误吗?
我在 SonarCloud 中查看代码分析的详细信息时遇到问题。
我正在工作的是带有 Coverlet 的 .Net Core 应用程序。我确实看到结果已上传并且覆盖范围已显示。但是,我看不到仪表板,“代码”选项卡不显示代码,并且“度量”也不提供详细信息。
我的 Github 项目已链接,正如我所看到的,结果已上传。我想知道为什么我看不到代码和详细的覆盖范围。我不熟悉 .NET 代码,并且 SonarQube 已经安装在其他项目中,所以我想知道我是否忘记了什么。我可以看到针对特定分支的两个 PR 构建的结果。
所以我的问题是如何查看细节。难道只有我合并后它才显示在主分支上吗?
这是一个 .NET Core 项目。我对 .NET Framework 应用程序也有同样的问题。
我最近开始使用C#
我们现有的一个遗留系统。我试图弄清楚这个遗留系统的代码覆盖率是多少。这是我的Sample.UnitTests.csproj
文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.2.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.2.1" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="nunit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="WireMock.Net" Version="1.0.4.17" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Sample/Sample.csproj" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.2.10" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,发现我们可以使用coverlet
它来生成cobertura
样式报告。我完全按照我的 …
我有一个 .Net Core 应用程序,其中有一个 .Net Standard 2.1 项目。我正在使用 Coverlet 来获取 Cobertura 格式的代码覆盖率。
我在所有测试项目中使用“coverlet.msbuild”nuget 包。
我想在程序集级别添加 [ExcludeFromCodeCoverage] 属性,以便 Coverlet 在执行分析时忽略此项目。
我在 .Net Core / .Net Standard 项目中找不到 AssemblyInfo.cs 文件。
我尝试在 proejct 的 .csproj 文件中添加以下标签
<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
但仍然没有运气。
对我来说唯一的解决方法是在所有类文件中手动添加 [ExcludeFromCodeCoverage] 属性,这不是最好的方法。
我有一个大型 .NET Framework 解决方案,并希望开始收集代码覆盖率数据作为我们构建管道的一部分(以及在我们的本地开发人员机器上)。
在 Coverlet GitHub 页面上,它说它支持 .NET Framework 项目,但所有示例都使用dotnet test
CLI 命令。
是否可以为此使用 Coverlet 还是我应该查看 OpenCover 之类的东西?
如何使用coverlet
和从代码覆盖率报告中排除方法reportgenerator
。排除整个命名空间可以.runsettings
按预期工作,但使用[ExcludeFromCodeCoverage]
属性会排除整个文件,而不仅仅是目标方法。有关我在 中尝试过的内容,请参阅下面的评论.runsettings
。
相关.runsettings
线路:
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector>
<Configuration>
<Format>lcov</Format>
<Include>[*]*</Include>
<Exclude>
<!-- excluded namespaces -->
</Exclude>
<!-- excludes entire file from coverage -->
<ExcludeByAttribute>Obsolete, GeneratedCodeAttribute, CompilerGeneratedAttribute,ExcludeFromCodeCoverage</ExcludeByAttribute>
<!-- included & reported as uncovered -->
<ExcludeByAttribute> ExcludeFromCodeCoverageAttribute </ExcludeByAttribute>
<SingleHit>true</SingleHit>
<UseSourceLink>true</UseSourceLink>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- included and reported as uncovered -->
<CodeCoverage>
<Attributes>
<Exclude>
<Attribute> ^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$</Attribute>
</Exclude>
</Attributes>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
Run Code Online (Sandbox Code Playgroud)
更新:这似乎是最近解决的问题coverlet
。更新解决了该问题。https://github.com/coverlet-coverage/coverlet/issues/809
我想从代码覆盖率计算中排除所有自动生成的迁移文件。我无法更改dotnet test
构建管道中的命令,所以我想我唯一的朋友就是属性[ExcludeFromCodeCoverage]
。
棘手的部分是,每次添加新的迁移时,我都需要手动检查所有生成的文件,并确保我[ExcludeFromCodeCoverage]
在所有生成的类上都有属性,这很好,但我想知道是否有更好的解决方案,我可以一次性完成全部?
迁移文件
[ExcludeFromCodeCoverage] // Manually added everytime
partial class Initial : Migration
Run Code Online (Sandbox Code Playgroud)
和 ModelSnapshot 文件
[ExcludeFromCodeCoverage] // This gets removed everytime snapshot is updated
[DbContext(typeof(MyContext))]
partial class MyContextModelSnapshot : ModelSnapshot
Run Code Online (Sandbox Code Playgroud)
对于快照文件,由于类名始终相同,我可以创建一个单独的文件MyContextModelSnapshot.CodeCoverage.cs
文件并将属性放在部分类上,但是迁移文件有解决方案吗?
coverlet.msbuild
如果重要的话我正在合作。
我在用
coverlet.msbuild v3.2.0
coverlet.collector v3.2.0
和dotnet sdk v6.0.402
当我在 powershell 中运行此测试命令时(之前运行的恢复和构建)
dotnet test --no-build --no-restore --collect:"XPlat Code Coverage" /p:Configuration=$Cfg /p:CollectCoverage=true /p:CoverletOutput=.\CodeCoverage\ --% /p:CoverletOutputFormat=\"cobertura,opencover\"
Run Code Online (Sandbox Code Playgroud)
报告文件
coverage.cobertura.xml
coverage.opencover.xml
创建后,powershell 输出如下:
Test run for C:\Users\MyUser\repos\My.Project\Specs\bin\Release\net472\My.Project.Specs.dll (.NETFramework,Version=v4.7.2)
Microsoft (R) Test Execution Command Line Tool Version 17.3.1 (x64)
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Attachments:
C:\Users\MyUser\repos\My.Project\Specs\TestResults\dbe8cb53-3ec5-4227-a231-3bfedf94694f\coverage.cobertura.xml
Passed! - Failed: 0, Passed: 14, Skipped: 0, Total: 14, Duration: 910 …
Run Code Online (Sandbox Code Playgroud) coverlet ×10
c# ×4
.net-core ×3
.net ×2
unit-testing ×2
.net-4.8 ×1
asp.net-core ×1
azure ×1
azure-devops ×1
dotnet-test ×1
gitlab-ci ×1
lcov ×1
mstest ×1
nunit ×1
powershell ×1
sonarcloud ×1
sonarqube ×1
xunit ×1