.NET Core中的自动单元测试以及Visual Studio Code中的代码覆盖率

本文介绍了如何使用VS Code、Coverlet和xUnit实现.NET Core的自动单元测试,并结合Visual Studio Code扩展实现代码覆盖率。通过Coverlet集成MSBuild,在执行`dotnet test`时自动收集覆盖率信息,配合Coverage Gutters扩展展示测试覆盖率。同时,文章讨论了.NET Core Test Explorer对提升开发体验的帮助,以及如何配置`.vscode/tasks.json`文件以优化测试流程。
摘要由CSDN通过智能技术生成

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, CoverletxUnit和这些Visual Studio Code扩展

Here's what we came up with.

这就是我们想到的。

Auto testing, code coverage, line coloring, test explorers, all in VS Code

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

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

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

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,新项目向导以及列中的格式代码。

翻译自: https://www.hanselman.com/blog/automatic-unit-testing-in-net-core-plus-code-coverage-in-visual-studio-code

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值