I was talking to Toni Edward Solarin on Skype yesterday about his open source spike (early days) of Code Coverage for .NET Core called "coverlet." There's a few options out there for cobbling together .NET Core Code Coverage but I wanted to see if I could use the lightest tools I could find and make a "complete" solution for Visual Studio Code that would work for .NET Core cross platform. I put my own living spike of a project up on GitHub.
昨天,我在Skype上与Toni Edward Solarin谈论了他的.NET Core代码覆盖率的开源高峰期(早期),称为“ coverlet” 。 有一些选项可以将.NET Core代码覆盖范围整合在一起,但是我想看看是否可以使用我能找到的最轻巧的工具,并为适用于.NET Core跨平台的Visual Studio Code制定一个“完整”的解决方案。 我在GitHub上放置了自己的项目生活尖峰。
Now, keeping in mind that Toni's project is just getting started and (as of the time of this writing) currently supports line and method coverage, and branch coverage is in progress, this is still a VERY compelling developer experience.
现在,请记住,Toni的项目才刚刚开始,并且(截至撰写本文时)当前支持行和方法覆盖范围,并且分支覆盖范围正在进行中,这仍然是非常引人注目的开发人员经验。
Using VS Code, Coverlet, xUnit, plus these Visual Studio Code extensions
使用VS Code, Coverlet , xUnit和这些Visual Studio Code扩展
Coverage Gutters - Reads in the lcov.info file (name matters) and highlights lines with color
覆盖范围-读取lcov.info文件(名称很重要)并用颜色突出显示行
.NET Core Test Explorer - Discovers tests and gives you a nice explorer.
.NET Core Test Explorer-发现测试并为您提供一个不错的资源管理器。
Coverlet - The start of .NET Core Code Coverage
Coverlet-.NET Core代码覆盖范围的开始
Here's what we came up with.
这就是我们想到的。
![Auto testing, code coverage, line coloring, test explorers, all in VS Code Auto testing, code coverage, line coloring, test explorers, all in VS Code](https://i-blog.csdnimg.cn/blog_migrate/3aff2dd02085726dccdc287eae16ecc8.png)
There's a lot going on here but take a moment and absorb the screenshot of VS Code above.
这里有很多事情要做,但花点时间吸收上面的VS Code的屏幕截图。
Our test project is using xunit and the xunit runner that integrates with .NET Core as expected.
我们的测试项目使用xunit和按预期与.NET Core集成的xunit运行程序。
- That means we can just "dotnet test" and it'll build and run tests. 这意味着我们可以进行“ dotnet测试”,它将构建并运行测试。
Our test project is using xunit and the xunit runner that integrates with .NET Core as expected.
我们的测试项目使用xunit和按预期与.NET Core集成的xunit运行程序。
Added coverlet, which integrates with MSBuild and automatically runs when you "dotnet test" if you "dotnet test /p:CollectCoverage=true"
添加了Coverlet ,它与MSBuild集成,并且如果您“ dotnet测试/ p:CollectCoverage = true”,则在“ dotnet测试”时自动运行
- (I think this should command line switch should be more like --coverage" but there may be an MSBuild limitation here.)(我认为命令行切换应该更像是--coverage,但是这里可能有一个MSBuild限制。)
Added coverlet, which integrates with MSBuild and automatically runs when you "dotnet test" if you "dotnet test /p:CollectCoverage=true"
添加了Coverlet ,它与MSBuild集成,并且如果您“ dotnet测试/ p:CollectCoverage = true”,则在“ dotnet测试”时自动运行
I'm interested in "The Developer's Inner Loop." . That means I want to have my tests open, my code open, and as I'm typing I want the solution to build, run tests, and update code coverage automatically the way Visual Studio proper does auto-testing, but in a more Rube Goldbergian way. We're close with this setup, although it's a little slow.
我对“开发人员的内循环”感兴趣。 。 这意味着我想打开我的测试,打开我的代码,并且当我输入内容时,我希望该解决方案以Visual Studio适当的自动测试方式自动生成,运行测试并更新代码覆盖率,但是要使用更多的Rube。戈德伯格的方式。 尽管有点慢,但我们已经结束了此设置。
Coverlet can produce opencover, lcov, or json files as a resulting output file. You can then generate detailed reports from this. There is a language agnostic VS Code Extension called Coverage Gutters that can read in lcov files and others and highlight line gutters with red, yellow, green to show test coverage. Those lcov files look like this, showing file names, file numbers, coverage, and number of exceptions.
Coverlet可以生成opencover,lcov或json文件作为结果输出文件。 然后,您可以据此生成详细的报告。 有一个与语言无关的VS代码扩展,称为Coverage Gutters ,可以读取lcov文件和其他文件,并用红色,黄色,绿色突出显示线槽以显示测试覆盖率。 这些lcov文件看起来像这样,显示文件名,文件编号,覆盖范围和例外数量。
SF:C:\github\hanselminutes-core\hanselminutes.core\Constants.cs
DA:3,0
end_of_record
SF:C:\github\hanselminutes-core\hanselminutes.core\MarkdownTagHelper.cs
DA:21,5
DA:23,5
DA:49,5
I should be able to pick the coverage file manually with the extension, but due to a small bug, it's easier to just tell Coverlet to generate a specific file name in a specific format.
我应该可以使用扩展名手动选择Coverage文件,但是由于存在一个小错误,告诉Coverlet生成特定格式的特定文件会更容易。
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info .\my.tests
The lcov.info files then watched by the VSCode Coverage Gutters extension and updates as the file changes if you click watch in the VS Code Status Bar.
然后,如果单击“ VS代码状态栏”中的“监视”,则lccode.info文件将由VSCode Coverage Gutters扩展监视,并随着文件的更改而更新。
You can take it even further if you add "dotnet watch test" which will compile and re-run tests if code changes:
如果添加“ dotnet watch test”,则可以更进一步,如果代码更改,它将编译并重新运行测试:
dotnet watch --project .\my.tests test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info
I can run "WatchTests.cmd" in another terminal, or within the VS Code integrated terminal.
我可以在另一个终端中或在VS Code集成终端中运行“ WatchTests.cmd”。
![tests automatically running as code changes tests automatically running as code changes](https://i-blog.csdnimg.cn/blog_migrate/2483512fc2aeab16d605efca1f34a7c5.png)
NOTE: If you're doing code coverage you'll want to ensure your tests and tested assembly are NOT the same file. You might be able to get it to work but it's easier to keep things separate.
注意:如果要覆盖代码,则需要确保测试和经过测试的程序集不是同一文件。 您也许可以使它正常工作,但将各个部分分开更容易。
Next, add in the totally under appreciated .NET Core Test Explorer extension (this should have hundreds of thousands of downloads - it's criminal) to get this nice Test Explorer pane:
接下来,添加完全受人赞赏的.NET Core Test Explorer扩展(应该有成千上万的下载-这是犯罪的),以得到这个漂亮的Test Explorer窗格:
![A Test Explorer tree view in VS Code for NET Core projects A Test Explorer tree view in VS Code for NET Core projects](https://i-blog.csdnimg.cn/blog_migrate/e89f30753ac0d51dc0dc4f685e8c44e1.png)
Even better, .NET Test Explorer lights up some "code lens" style interfaces over each test as well as a green checkmark for passing tests. Having "debug test" available for .NET Core is an absolute joy.
更好的是,.NET Test Explorer会在每个测试中点亮一些“代码镜头”样式的界面,并为通过测试提供绿色的选中标记。 为.NET Core提供“调试测试”绝对是一件乐事。
Finally we make some specific improvements to the .vscode/tasks.json file that drives much of VS Code's experience with our app. The "BUILD" label is standard but note both the custom "test" and "testwithcoverage" labels, as well as the added group with kind: "test."
最后,我们对.vscode / tasks.json文件进行了一些特定的改进,从而提高了VS Code在我们的应用程序中的许多经验。 “ BUILD”标签是标准标签,但请注意自定义的“ test”标签和“ testwithcoverage”标签,以及添加的种类为“ test”的组。
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/hanselminutes.core.tests/hanselminutes.core.tests.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "test",
"command": "dotnet",
"type": "process",
"args": [
"test",
"${workspaceFolder}/hanselminutes.core.tests/hanselminutes.core.tests.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "test with coverage",
"command": "dotnet",
"type": "process",
"args": [
"test",
"/p:CollectCoverage=true",
"/p:CoverletOutputFormat=lcov",
"/p:CoverletOutput=./lcov.info",
"${workspaceFolder}/hanselminutes.core.tests/hanselminutes.core.tests.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
},
]
}
This lets VS Code know what's for building and what's for testing, so if I use the Command Palette to "Run Test" then I'll get this dropdown that lets me run tests and/or update coverage manually if I don't want the autowatch stuff going.
这使VS Code知道要进行的构建和要进行的测试,因此,如果我使用命令面板来“运行测试”,那么我将获得此下拉菜单,如果我不想这样做,可以手动运行测试和/或更新覆盖率。自动手表的东西去。
![Test or Test with Coverage Test or Test with Coverage](https://i-blog.csdnimg.cn/blog_migrate/8ef6861a75bb581e4aac107a4708e5fe.png)
Again, all this is just getting started but I've applied it to my Podcast Site that I'm currently rewriting and the experience is very smooth!
再说一次,所有这些都刚刚开始,但是我已经将其应用到我目前正在重写的Podcast网站上,体验非常流畅!
Here's a call to action for you! Toni is just getting started on Coverlet and I'm sure he'd love some help. Head over to the Coverlet github and don't just file issues and complain! This is an opportunity for you to get to know the deep internals of .NET and create something cool for the larger community.
这是您的号召性用语! Toni刚开始使用Coverlet ,我相信他会喜欢一些帮助的。 前往Coverlet github,不要只是提出问题并抱怨! 这是一个机会,您可以了解.NET的深入内部知识,并为更广泛的社区创建一些很棒的东西。
What are your thoughts?
你都有些什么想法呢?
Sponsor: Get the latest JetBrains Rider for debugging third-party .NET code, Smart Step Into, more debugger improvements, C# Interactive, new project wizard, and formatting code in columns.
赞助商:获取最新的JetBrains Rider,用于调试第三方.NET代码,Smart Step Into,更多调试器改进,C#Interactive,新项目向导以及列中的格式代码。