如何运行单元测试
dotnet test
如果我在一个代码库中有多个测试库?
我可以跑步
dotnet测试
,它将查找并运行所有测试,甚至跨多个库,但它会独立运行并报告每个测试库:
$ dotnet test
Test run for C:\Users\mark\Documents\Redacted.Test\bin\Debug\netcoreapp2.1\Redacted.Test.dll(.NETCoreApp,Version=v2.1)
Test run for C:\Users\mark\Documents\Redacted\Redacted.SqlAccess.Test\bin\Debug\netcoreapp2.1\Redacted.SqlAccess.Test.dll(.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 16.2.0-preview-20190606-02
Copyright (c) Microsoft Corporation. All rights reserved.
Microsoft (R) Test Execution Command Line Tool Version 16.2.0-preview-20190606-02
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
Starting test execution, please wait...
Test Run Successful.
Total tests: 59
Passed: 59
Total time: 3.1779 Seconds
Test run for C:\Users\mark\Documents\Redacted\Redacted.RestApi.Tests\bin\Debug\netcoreapp2.1\Redacted.RestApi.Tests.dll(.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 16.2.0-preview-20190606-02
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
Test Run Successful.
Total tests: 99
Passed: 99
Total time: 9.8155 Seconds
Test Run Successful.
Total tests: 25
Passed: 25
Total time: 21.2894 Seconds
在这个例子中,有两个测试库,所以我得到两个测试结果输出。
如果已经编译了代码,这可能会正常工作,但是在一个干净的构建中,编译器会有大量的输出。这很容易导致
测试运行摘要滚动到屏幕的可见部分
.
如果测试运行失败,这是个问题。
如何将所有单元测试折叠为单个通过/失败摘要?
例如,在.NET 4.x上,我可以使用xunit.net的控制台运行程序将所有测试库作为单个套件运行:
$ ./packages/xunit.runner.console.2.4.0/tools/net461/xunit.console BookingApi.UnitTests/bin/Debug/Ploeh.Samples.Booking
Api.UnitTests.dll BookingApi.SqlTests/bin/Debug/Ploeh.Samples.BookingApi.SqlTests.dll
xUnit.net Console Runner v2.4.0 (64-bit Desktop .NET 4.6.1, runtime: 4.0.30319.42000)
Discovering: Ploeh.Samples.BookingApi.UnitTests
Discovered: Ploeh.Samples.BookingApi.UnitTests
Starting: Ploeh.Samples.BookingApi.UnitTests
Finished: Ploeh.Samples.BookingApi.UnitTests
Discovering: Ploeh.Samples.BookingApi.SqlTests
Discovered: Ploeh.Samples.BookingApi.SqlTests
Starting: Ploeh.Samples.BookingApi.SqlTests
Finished: Ploeh.Samples.BookingApi.SqlTests
=== TEST EXECUTION SUMMARY ===
Ploeh.Samples.BookingApi.SqlTests Total: 3, Errors: 0, Failed: 0, Skipped: 0, Time: 3.816s
Ploeh.Samples.BookingApi.UnitTests Total: 7, Errors: 0, Failed: 0, Skipped: 0, Time: 0.295s
-- - - - ------
GRAND TOTAL: 10 0 0 0 4.111s (5.565s)
注意这是如何产生的
一个总结
在屏幕的底部,这样我可以立即看到我的测试是否通过或失败。