asp.net 使用dotnet test/nunit console runner打印所有测试用例沿着通过/失败

6rqinv9w  于 2023-02-01  发布在  .NET
关注(0)|答案(1)|浏览(223)

我希望打印所有测试用例沿着状态(通过/未通过),如下所示:

TestCaseOne --> Passed
TestCaseTwo --> Failed
TestCaseThree --> Passed

我已经从这个链接console runner test尝试过了,但是它只打印测试名称,没有状态。
我也尝试了同样的“dotnet测试”从这个链接dotnet test
是否有方法打印如上所示的输出?

c3frrgcw

c3frrgcw1#

编辑:我意识到你是在一个非常具体的输出格式。

dotnet test --logger "console;verbosity=normal"

会产生如下的结果:

PS C:\dev\mstest1> dotnet test --logger "console;verbosity=normal"
  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\7.0.100-rc.1.22431.12\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(219,5): message NETSDK1057: You are using a preview ver  
sion of .NET. See: https://aka.ms/dotnet-support-policy [C:\dev\mstest1\mstest1.csproj]
  mstest1 -> C:\dev\mstest1\bin\Debug\net7.0\mstest1.dll
Test run for C:\dev\mstest1\bin\Debug\net7.0\mstest1.dll (.NETCoreApp,Version=v7.0)
Microsoft (R) Test Execution Command Line Tool Version 17.4.0-preview-20220813-01 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
  Passed TestMethod1 [2 ms]
  Failed TestMethod2 [16 ms]
  Error Message:
   Assert.Fail failed. x
  Stack Trace:
     at mstest1.UnitTest1.TestMethod2() in C:\dev\mstest1\UnitTest1.cs:line 15
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

  Skipped TestMethod3

Test Run Failed.
Total tests: 3
     Passed: 1
     Failed: 1
    Skipped: 1
 Total time: 0.8217 Seconds

你感兴趣的部分是:

Passed TestMethod1 [2 ms]
  Failed TestMethod2 [15 ms]
  Skipped TestMethod3

我们可以通过grepawk来传递它,以获得所需的格式:

$ dotnet test --logger "console;verbosity=normal" | grep '^  .*' | grep -e 'Passed \|Failed \|Skipped ' | awk '{print $2" --> "$1 }'
Test Run Failed.
TestMethod1 --> Passed
TestMethod2 --> Failed
TestMethod3 --> Skipped

相关问题