From 6b72e1b83e5617ff706489953b24f7b770e56e68 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Mon, 1 Feb 2021 13:28:45 +0100 Subject: [PATCH 1/4] Fix divide by zero in HTML logger --- .../HtmlLogger.cs | 2 +- .../HtmlLoggerTests.cs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs index 0910bd0213..2c6d7c6f43 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs @@ -282,7 +282,7 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) PassedTests = PassedTests, TotalTests = TotalTests, SkippedTests = SkippedTests, - PassPercentage = (PassedTests * 100) / TotalTests, + PassPercentage = (PassedTests * 100) / (TotalTests >= 1 ? TotalTests : 1), TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests), }; if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue)) diff --git a/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs index d53ace86bd..2788549783 100644 --- a/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs @@ -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(), FileMode.Create, FileAccess.ReadWrite)).Callback((x, y, z) => + { + }).Returns(new Mock().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"); From 3d33960615dca5b82e9375874f00eb3c2e8c1cd7 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Mon, 1 Feb 2021 14:16:22 +0100 Subject: [PATCH 2/4] Address PR feedback --- src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs index 2c6d7c6f43..0da5618b53 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs @@ -282,7 +282,7 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) PassedTests = PassedTests, TotalTests = TotalTests, SkippedTests = SkippedTests, - PassPercentage = (PassedTests * 100) / (TotalTests >= 1 ? TotalTests : 1), + PassPercentage = TotalTests != 0 ? PassedTests * 100 / TotalTests : 0, TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests), }; if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue)) From be07219969f69c8694dfb62c4c67c3fb93ac76ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 1 Feb 2021 14:58:13 +0100 Subject: [PATCH 3/4] Update src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs Co-authored-by: Sanan Yuzbashiyev --- .../HtmlLogger.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs index 0da5618b53..3666afadd1 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs @@ -282,7 +282,7 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) PassedTests = PassedTests, TotalTests = TotalTests, SkippedTests = SkippedTests, - PassPercentage = TotalTests != 0 ? PassedTests * 100 / TotalTests : 0, + PassPercentage = TotalTests == 0 ? 0 : PassedTests * 100 / TotalTests TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests), }; if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue)) @@ -441,4 +441,4 @@ internal string GetFormattedDurationString(TimeSpan duration) return time.Count == 0 ? "< 1ms" : string.Join(" ", time); } } -} \ No newline at end of file +} From 117ee66f680f765d735af70c9cc2b9d1d2c849fe Mon Sep 17 00:00:00 2001 From: nohwnd Date: Mon, 1 Feb 2021 15:19:26 +0100 Subject: [PATCH 4/4] Fix comma --- src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs index 3666afadd1..545c110569 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs @@ -282,7 +282,7 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) PassedTests = PassedTests, TotalTests = TotalTests, SkippedTests = SkippedTests, - PassPercentage = TotalTests == 0 ? 0 : 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))