命令行:使用dotnet watch test对.NET Core 1.0和XUnit.net进行连续测试

I've installed .NET Core 1.0 on my machine. Let's see if I can get a class library and tests running and compiling automatically using only the command line. (Yes, some of you are freaked out by my (and other folks') appreciation of a nice, terse command line. Don't worry. You can do all this with a mouse if you want. I'm just enjoying the CLI.

我已经在计算机上安装了.NET Core 1.0。 让我们看看是否可以仅使用命令行来获得类库并测试运行和自动编译。 (是的,你们中的一些人对我(和其他人)对一个简洁简洁的命令行的欣赏感到惊讶。不用担心。如果需要,您可以用鼠标完成所有这些操作。我只是在享受CLI 。

NOTE: This is considerably updated from the project.json version in 2016.

注意:这是从2016年的project.json版本进行的相当大的更新。

First, I installed from http://dot.net/core. This should all work on Windows, Mac, or Linux.

首先,我从http://dot.net/core安装。 这一切都可以在Windows,Mac或Linux上运行。

C:\> md testexample & cd testexample

C:\testexample> dotnet new sln
Content generation time: 33.0582 ms
The template "Solution File" created successfully.

C:\testexample> dotnet new classlib -n mylibrary -o mylibrary
Content generation time: 40.5442 ms
The template "Class library" created successfully.

C:\testexample> dotnet new xunit -n mytests -o mytests
Content generation time: 87.5115 ms
The template "xUnit Test Project" created successfully.

C:\testexample> dotnet sln add mylibrary\mylibrary.csproj
Project `mylibrary\mylibrary.csproj` added to the solution.

C:\testexample> dotnet sln add mytests\mytests.csproj
Project `mytests\mytests.csproj` added to the solution.

C:\testexample> cd mytests

C:\testexample\mytests> dotnet add reference ..\mylibrary\mylibrary.csproj
Reference `..\mylibrary\mylibrary.csproj` added to the project.

C:\testexample\mytests> cd ..

C:\testexample> dotnet restore
Restoring packages for C:\Users\scott\Desktop\testexample\mytests\mytests.csproj...
Restoring packages for C:\Users\scott\Desktop\testexample\mylibrary\mylibrary.csproj...
Restore completed in 586.73 ms for C:\Users\scott\Desktop\testexample\mylibrary\mylibrary.csproj.
Installing System.Diagnostics.TextWriterTraceListener 4.0.0.
...SNIP...
Installing Microsoft.NET.Test.Sdk 15.0.0.
Installing xunit.runner.visualstudio 2.2.0.
Installing xunit 2.2.0.
Generating MSBuild file C:\Users\scott\Desktop\testexample\mytests\obj\mytests.csproj.nuget.g.props.
Generating MSBuild file C:\Users\scott\Desktop\testexample\mytests\obj\mytests.csproj.nuget.g.targets.
Writing lock file to disk. Path: C:\Users\scott\Desktop\testexample\mytests\obj\project.assets.json
Installed:
16 package(s) to C:\Users\scott\Desktop\testexample\mytests\mytests.csproj

C:\testexample> cd mytests & dotnet test

Build started, please wait...
Build completed.

Test run for C:\testexample\mytests\bin\Debug\netcoreapp1.1\mytests.dll(.NETCoreApp,Version=v1.1)
Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation. All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:00.5539676] Discovering: mytests
[xUnit.net 00:00:00.6867799] Discovered: mytests
[xUnit.net 00:00:00.7341661] Starting: mytests
[xUnit.net 00:00:00.8691063] Finished: mytests

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.8329 Seconds

Of course, I'm testing nothing yet but pretend there's a test in the tests.cs and something it's testing (that's why I added a reference) in the library.cs, OK?

当然,我什么都没有测试,但假装在tests.cs中有一个测试,并且在library.cs中有它正在测试的东西(这就是为什么我添加了引用),好吗?

Now I want to have my project build and tests run automatically as I make changes to the code. I can't "dotnet add tool" yet so I'll add this line to my test's project file:

现在,我想让我的项目构建并在对代码进行更改时自动运行测试。 我还不能“ dotnet add tool”,所以我将这一行添加到测试的项目文件中:

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />
</ItemGroup>

Like this:

像这样:

Adding <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="1.0.0" />

Then I just dotnet restore to bring in the tool.

然后,我只是通过dotnet restore来引入该工具。

In order to start the tests, I don't write dotnet test, I run "dotnet watch test." The main command is watch, and then WATCH calls TEST. You can also dotnet watch run, etc.

为了开始测试,我不编写dotnet测试,而是运行“ dotnet watch test” 。 主要命令是watch,然后WATCH调用TEST。 您还可以运行dotnet watch run等。

NOTE: There's a color bug using only cmd.exe so on "DOS" you'll see some ANSI chars. That should be fixed in a minor release soon - the PR is in and waiting. On bash or PowerShell things look fin.

注意:仅使用cmd.exe有一个颜色错误,因此在“ DOS”上您将看到一些ANSI字符。 应该很快在次要版本中解决该问题-PR正在等待。 在bash或PowerShell上一切正常。

In this screenshot, you can see as I make changes to my test and hit save, the DotNetWatcher Tool sees the change and restarts my app, recompiles, and re-runs the tests.

在此屏幕截图中,您可以看到我对测试进行更改并单击保存时,DotNetWatcher工具会看到更改并重新启动我的应用程序,重新编译并重新运行测试。

Test Run Successful

All this was done from the command line. I made a solution file, made a library project and a test project, made the test project reference the library, then built and ran the tests. If I could add the tool from the command line I wouldn't have had to manually touch the project file at all.

所有这些都是从命令行完成的。 我创建了一个解决方案文件,创建了一个库项目和一个测试项目,使测试项目引用了库,然后构建并运行了测试。 如果我可以从命令行添加该工具,则完全不必手动触摸项目文件。

Again, to be sure, all this is stuff you can (and do) do in Visual Studio manually all the time. But I'll race you anytime. ;)

同样,可以肯定的是,所有这些都是您可以在Visual Studio中始终手动进行的事情。 但是我会随时比赛。 ;)

Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test, build and debug ASP.NET, .NET Framework, .NET Core, or Unity applications. Learn more and get access to early builds!

赞助商:查看JetBrains Rider :一个新的跨平台.NET IDE 。 编辑,重构,测试,构建和调试ASP.NET,.NET Framework,.NET Core或Unity应用程序。 了解更多信息并获得早期版本!

翻译自: https://www.hanselman.com/blog/command-line-using-dotnet-watch-test-for-continuous-testing-with-net-core-10-and-xunitnet

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值