user19540656
user19540656

Reputation:

Print all test cases along with passed/failed using dotnet test/nunit console runner

I'm looking to print all the test cases along with their status(pass/fail) like below:

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

I have tried this from this link console runner test , but it only prints the test names, without the status.

I also tried the same with "dotnet test" from this link dotnet test

Is there a way to print the output as shown above?

Upvotes: 1

Views: 1128

Answers (1)

tmaj
tmaj

Reputation: 34947

EDIT: I realised that you are after a very specific output format.

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

will produce something like the following:

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

The part interesting for you is:

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

We could pass this via grep and awk to achieve the desired format:

$ 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

Upvotes: 1

Related Questions