Skip to content

Commit

Permalink
Fix divide by zero in HTML logger (#2723)
Browse files Browse the repository at this point in the history
* Fix divide by zero in HTML logger

* Address PR feedback

* Update src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs

Co-authored-by: Sanan Yuzbashiyev <Sanan07@users.noreply.github.com>

* Fix comma

Co-authored-by: Sanan Yuzbashiyev <Sanan07@users.noreply.github.com>
  • Loading branch information
nohwnd and Sanan07 committed Feb 1, 2021
1 parent a523efd commit 3993efb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
PassedTests = PassedTests,
TotalTests = TotalTests,
SkippedTests = SkippedTests,
PassPercentage = (PassedTests * 100) / TotalTests,
PassPercentage = TotalTests == 0 ? 0 : PassedTests * 100 / TotalTests,
TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests),
};
if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue))
Expand Down Expand Up @@ -441,4 +441,4 @@ internal string GetFormattedDurationString(TimeSpan duration)
return time.Count == 0 ? "< 1ms" : string.Join(" ", time);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,20 @@ public void TestCompleteHandlerShouldWriteToXmlSerializerCorrectly()
Assert.IsTrue(htmlLogger.HtmlFilePath.Contains(".html"));
}

[TestMethod]
public void TestCompleteHandlerShouldNotDivideByZeroWhenThereAre0TestResults()
{

this.mockFileHelper.Setup(x => x.GetStream(It.IsAny<string>(), FileMode.Create, FileAccess.ReadWrite)).Callback<string, FileMode, FileAccess>((x, y, z) =>
{
}).Returns(new Mock<Stream>().Object);

this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero));

Assert.AreEqual(0, this.htmlLogger.TestRunDetails.Summary.TotalTests);
Assert.AreEqual(0, this.htmlLogger.TestRunDetails.Summary.PassPercentage);
}

private static TestCase CreateTestCase(string testCaseName)
{
return new TestCase(testCaseName, new Uri("some://uri"), "DummySourceFileName");
Expand Down

0 comments on commit 3993efb

Please sign in to comment.