Skip to content

Commit

Permalink
Cli experience changes (#1960)
Browse files Browse the repository at this point in the history
* UX changes
- Adding the time for individual tests.
- Use indicators instead of Passed/Failed
- Formatted Summary.
- Adding  the < 1ms scenario.
  • Loading branch information
singhsarab authored Apr 2, 2019
1 parent 30bf95c commit c4ad411
Show file tree
Hide file tree
Showing 21 changed files with 1,260 additions and 1,260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public static void Information(this IOutput output, bool appendPrefix, ConsoleCo
});
}

/// <summary>
/// Write string with a given console color
/// </summary>
/// <param name="output">Output instance the method is being invoked with.</param>
/// <param name="message">Message to be written</param>
/// <param name="level">OutputLevel</param>
/// <param name="foregroundColor">Console color for the output message</param>
public static void Write(this IOutput output, string message, OutputLevel level, ConsoleColor foregroundColor)
{
SetColorForAction(foregroundColor, () =>
{
output.Write(message, level);
});
}

/// <summary>
/// Formats the message.
/// </summary>
Expand Down
135 changes: 100 additions & 35 deletions src/vstest.console/Internal/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ internal class ConsoleLogger : ITestLoggerWithParameters
#region Constants
private const string TestMessageFormattingPrefix = " ";

/// <summary>
/// Prefix used for formatting the result output
/// </summary>
private const string TestResultPrefix = " ";

/// <summary>
/// Unicode for tick
/// </summary>
private const char PassedTestIndicator = '\u221a';

/// <summary>
/// Indicator for failed tests
/// </summary>
private const char FailedTestIndicator = 'X';

/// <summary>
/// Indicated skipped and not run tests
/// </summary>
private const char SkippedTestIndicator = '!';

/// <summary>
/// Bool to decide whether Verbose level should be added as prefix or not in log messages.
/// </summary>
Expand Down Expand Up @@ -255,23 +275,24 @@ private static Collection<TestResultMessage> GetTestMessages(Collection<TestResu
/// </summary>
private static void DisplayFullInformation(TestResult result)
{

// Add newline if it is not in given output data.
var addAdditionalNewLine = false;

Debug.Assert(result != null, "a null result can not be displayed");
if (!String.IsNullOrEmpty(result.ErrorMessage))
{
addAdditionalNewLine = true;
Output.Information(false, ConsoleColor.Red, CommandLineResources.ErrorMessageBanner);
var errorMessage = String.Format(CultureInfo.CurrentCulture, "{0}{1}", TestMessageFormattingPrefix, result.ErrorMessage);
Output.Information(false, ConsoleColor.Red, string.Format("{0}{1}", TestResultPrefix, CommandLineResources.ErrorMessageBanner));
var errorMessage = String.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", TestResultPrefix, TestMessageFormattingPrefix, result.ErrorMessage);
Output.Information(false, ConsoleColor.Red, errorMessage);
}

if (!String.IsNullOrEmpty(result.ErrorStackTrace))
{
addAdditionalNewLine = false;
Output.Information(false, ConsoleColor.Red, CommandLineResources.StacktraceBanner);
var stackTrace = String.Format(CultureInfo.CurrentCulture, "{0}", result.ErrorStackTrace);
Output.Information(false, ConsoleColor.Red, string.Format("{0}{1}", TestResultPrefix, CommandLineResources.StacktraceBanner));
var stackTrace = String.Format(CultureInfo.CurrentCulture, "{0}{1}", TestResultPrefix, result.ErrorStackTrace);
Output.Information(false, ConsoleColor.Red, stackTrace);
}

Expand All @@ -283,7 +304,7 @@ private static void DisplayFullInformation(TestResult result)

if (!string.IsNullOrEmpty(stdOutMessages))
{
Output.Information(false, CommandLineResources.StdOutMessagesBanner);
Output.Information(false, string.Format("{0}{1}", TestResultPrefix, CommandLineResources.StdOutMessagesBanner));
Output.Information(false, stdOutMessages);
}
}
Expand All @@ -296,7 +317,7 @@ private static void DisplayFullInformation(TestResult result)

if (!string.IsNullOrEmpty(stdErrMessages))
{
Output.Information(false, ConsoleColor.Red, CommandLineResources.StdErrMessagesBanner);
Output.Information(false, ConsoleColor.Red, string.Format("{0}{1}", TestResultPrefix, CommandLineResources.StdErrMessagesBanner));
Output.Information(false, ConsoleColor.Red, stdErrMessages);
}
}
Expand All @@ -309,7 +330,7 @@ private static void DisplayFullInformation(TestResult result)

if (!string.IsNullOrEmpty(dbgTrcMessages))
{
Output.Information(false, CommandLineResources.DbgTrcMessagesBanner);
Output.Information(false, string.Format("{0}{1}", TestResultPrefix, CommandLineResources.DbgTrcMessagesBanner));
Output.Information(false, dbgTrcMessages);
}
}
Expand All @@ -322,7 +343,7 @@ private static void DisplayFullInformation(TestResult result)

if (!string.IsNullOrEmpty(addnlInfoMessages))
{
Output.Information(false, CommandLineResources.AddnlInfoMessagesBanner);
Output.Information(false, string.Format("{0}{1}", TestResultPrefix, CommandLineResources.AddnlInfoMessagesBanner));
Output.Information(false, addnlInfoMessages);
}
}
Expand Down Expand Up @@ -401,12 +422,19 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
// Update the test count statistics based on the result of the test.
this.testsTotal++;

string testDisplayName = e.Result.DisplayName;
var testDisplayName = e.Result.DisplayName;

if (string.IsNullOrWhiteSpace(e.Result.DisplayName))
{
testDisplayName = e.Result.TestCase.DisplayName;
}

string formattedDuration = this.GetFormattedDurationString(e.Result.Duration);
if (!string.IsNullOrEmpty(formattedDuration))
{
testDisplayName = string.Format("{0} [{1}]", testDisplayName, formattedDuration);
}

switch (e.Result.Outcome)
{
case TestOutcome.Skipped:
Expand All @@ -417,8 +445,8 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
break;
}

var output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.SkippedTestIndicator, testDisplayName);
Output.Warning(false, output);
Output.Write(string.Format("{0}{1} ", TestResultPrefix, SkippedTestIndicator), OutputLevel.Information, ConsoleColor.Yellow);
Output.WriteLine(testDisplayName, OutputLevel.Information);
if (this.verbosityLevel == Verbosity.Detailed)
{
DisplayFullInformation(e.Result);
Expand All @@ -434,9 +462,9 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
{
break;
}

var output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.FailedTestIndicator, testDisplayName);
Output.Information(false, ConsoleColor.Red, output);
Output.Write(string.Format("{0}{1} ", TestResultPrefix, FailedTestIndicator), OutputLevel.Information, ConsoleColor.Red);
Output.WriteLine(testDisplayName, OutputLevel.Information);
DisplayFullInformation(e.Result);
break;
}
Expand All @@ -446,8 +474,8 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
this.testsPassed++;
if (this.verbosityLevel == Verbosity.Normal || this.verbosityLevel == Verbosity.Detailed)
{
var output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.PassedTestIndicator, testDisplayName);
Output.Information(false, output);
Output.Write(string.Format("{0}{1} ", TestResultPrefix, PassedTestIndicator), OutputLevel.Information, ConsoleColor.Green);
Output.WriteLine(testDisplayName, OutputLevel.Information);
if (this.verbosityLevel == Verbosity.Detailed)
{
DisplayFullInformation(e.Result);
Expand All @@ -464,8 +492,8 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
break;
}

var output = string.Format(CultureInfo.CurrentCulture, CommandLineResources.NotRunTestIndicator, testDisplayName);
Output.Information(false, output);
Output.Write(string.Format("{0}{1} ", TestResultPrefix, SkippedTestIndicator), OutputLevel.Information, ConsoleColor.Yellow);
Output.WriteLine(testDisplayName, OutputLevel.Information);
if (this.verbosityLevel == Verbosity.Detailed)
{
DisplayFullInformation(e.Result);
Expand All @@ -479,6 +507,40 @@ private void TestResultHandler(object sender, TestResultEventArgs e)
this.progressIndicator?.Start();
}

private string GetFormattedDurationString(TimeSpan duration)
{
if (duration == default(TimeSpan))
{
return null;
}

var time = new List<string>();
if (duration.Hours > 0)
{
time.Add(duration.Hours + "h");
}

if (duration.Minutes > 0)
{
time.Add(duration.Minutes + "m");
}

if (duration.Hours == 0)
{
if (duration.Seconds > 0)
{
time.Add(duration.Seconds + "s");
}

if (duration.Milliseconds > 0 && duration.Minutes == 0)
{
time.Add(duration.Milliseconds + "ms");
}
}

return time.Count == 0 ? "< 1ms" : string.Join(" ", time);
}

/// <summary>
/// Called when a test run is completed.
/// </summary>
Expand All @@ -504,23 +566,6 @@ private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
Output.WriteLine(String.Empty, OutputLevel.Information);
}

// Output a summary.
if (testsTotal > 0)
{
string testCountDetails;

if (e.IsAborted || e.IsCanceled)
{
testCountDetails = string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummaryForCanceledOrAbortedRun, this.testsPassed, this.testsFailed, this.testsSkipped);
}
else
{
testCountDetails = string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummary, this.testsTotal, this.testsPassed, this.testsFailed, this.testsSkipped);
}

Output.Information(false, testCountDetails);
}

if (e.IsCanceled)
{
Output.Error(false, CommandLineResources.TestRunCanceled);
Expand All @@ -538,6 +583,26 @@ private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
Output.Information(false, ConsoleColor.Green, CommandLineResources.TestRunSuccessful);
}

// Output a summary.
if (testsTotal > 0)
{
string totalTestsformat = (e.IsAborted || e.IsCanceled) ? CommandLineResources.TestRunSummaryForCanceledOrAbortedRun : CommandLineResources.TestRunSummaryTotalTests;
Output.Information(false, string.Format(CultureInfo.CurrentCulture, totalTestsformat, testsTotal));

if (testsPassed > 0)
{
Output.Information(false, ConsoleColor.Green, string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummaryPassedTests, testsPassed));
}
if (testsFailed > 0)
{
Output.Information(false, ConsoleColor.Red, string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummaryFailedTests, testsFailed));
}
if (testsSkipped > 0)
{
Output.Information(false, ConsoleColor.Yellow, string.Format(CultureInfo.CurrentCulture, CommandLineResources.TestRunSummarySkippedTests, testsSkipped));
}
}

if (testsTotal > 0)
{
if (e.ElapsedTimeInRunningTests.Equals(TimeSpan.Zero))
Expand Down
Loading

0 comments on commit c4ad411

Please sign in to comment.