【问题标题】:How do I use dotnet test to run tests from multiple libraries with a single pass/fail summary如何使用 dotnet test 通过单个通过/失败摘要从多个库运行测试
【发布时间】:2019-12-24 03:37:01
【问题描述】:

如果我在代码库中有多个测试库,如何使用 dotnet test 运行单元测试?

我可以运行dotnet test,它甚至会跨多个库查找并运行所有测试,但它会运行并报告每个测试库独立运行:

$ 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)

注意这如何在屏幕底部生成单个摘要,以便我可以立即查看我的测试是通过还是失败。

【问题讨论】:

    标签: unit-testing .net-core xunit xunit.net


    【解决方案1】:

    使用dotnet vstest 运行多个程序集。

     PS> dotnet vstest --help
    
    Microsoft (R) Test Execution Command Line Tool Version 15.9.0
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Usage: vstest.console.exe [Arguments] [Options] [[--] <RunSettings arguments>...]]
    
    Description: Runs tests from the specified files.
    
    Arguments:
    
    [TestFileNames]
          Run tests from the specified files. Separate multiple test file names
          by spaces.
          Examples: mytestproject.dll
                    mytestproject.dll myothertestproject.exe
    ...
    

    请注意,此方法要求您指向已编译的程序集(而不是 dotnet test,后者希望您指向项目文件,并且可以选择先为您构建内容)。

    【讨论】:

    • “可以选择先为你构建东西” 听起来更好!但是,它似乎不会自动执行此操作,那么我该如何启用它呢?文档似乎没有说。
    • @MarkSeemann 你不能用dotnet vstest来做这件事,但是dotnet test会自动为你构建,如果它还没有构建(你必须将--no-build传递给dotnet test才能得到它停止这样做)。
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    相关资源
    最近更新 更多