diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Constants.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Constants.cs index fb91883432..1c0ac5394e 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Constants.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Constants.cs @@ -51,7 +51,12 @@ public static class Constants /// /// Log file parameter key /// - public const string LogFileNameKey = "LogFileName"; + public const string LogFileNameKey = "LogFileName"; + + /// + /// Log file prefix parameter key + /// + public const string LogFilePrefixKey = "LogFilePrefix"; public static readonly TestProperty ExecutionIdProperty = TestProperty.Register("ExecutionId", ExecutionIdPropertyIdentifier, typeof(Guid), TestPropertyAttributes.Hidden, typeof(TestResult)); diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs index 482f9408b0..4523f7519e 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/HtmlLogger.cs @@ -22,6 +22,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.HtmlLogger using HtmlResource = Resources.Resources; using HtmlLoggerConstants = Constants; + using NuGet.Frameworks; /// /// Logger for generating Html. @@ -137,6 +138,14 @@ public void Initialize(TestLoggerEvents events, Dictionary param } parametersDictionary = parameters; + + if (parameters.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && parameters.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue)) + { + var htmlParameterErrorMsg = string.Format(CultureInfo.CurrentCulture, HtmlResource.PrefixAndNameProvidedError); + EqtTrace.Error(htmlParameterErrorMsg); + throw new ArgumentException(htmlParameterErrorMsg); + } + this.Initialize(events, parameters[DefaultLoggerParameterNames.TestRunDirectory]); } @@ -153,7 +162,7 @@ public void TestMessageHandler(object sender, TestRunMessageEventArgs e) switch (e.Level) { case TestMessageLevel.Informational: - if(TestRunDetails.RunLevelMessageInformational == null) + if (TestRunDetails.RunLevelMessageInformational == null) { TestRunDetails.RunLevelMessageInformational = new List(); } @@ -273,11 +282,25 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) PassPercentage = (PassedTests * 100) / TotalTests, TotalRunTime = GetFormattedDurationString(e.ElapsedTimeInRunningTests), }; - var isLogFileNameParameterExists = parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey, - out string logFileNameValue); - if (isLogFileNameParameterExists && !string.IsNullOrWhiteSpace(logFileNameValue)) + if (this.parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFilePrefixKey, out string logFilePrefixValue) && !string.IsNullOrWhiteSpace(logFilePrefixValue)) + { + + var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework]; + if (framework != null) + { + framework = NuGetFramework.Parse(framework).GetShortFolderName(); + logFilePrefixValue = logFilePrefixValue + "_" + framework; + } + + logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + $".{HtmlLoggerConstants.HtmlFileExtension}"; + this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFilePrefixValue); + } + else { - HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue); + if (parametersDictionary.TryGetValue(HtmlLoggerConstants.LogFileNameKey, out string logFileNameValue) && !string.IsNullOrWhiteSpace(logFileNameValue)) + { + this.HtmlFilePath = Path.Combine(TestResultsDirPath, logFileNameValue); + } } PopulateHtmlFile(); @@ -410,7 +433,7 @@ internal string GetFormattedDurationString(TimeSpan duration) time.Add(duration.Milliseconds + "ms"); } } - } + } return time.Count == 0 ? "< 1ms" : string.Join(" ", time); } diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.Designer.cs b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.Designer.cs index 9c2bc33784..4c39a8ad6e 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.Designer.cs +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Extensions.HtmlLogger.Resources { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { @@ -77,5 +77,14 @@ internal static string HtmlLoggerError { return ResourceManager.GetString("HtmlLoggerError", resourceCulture); } } + + /// + /// Looks up a localized string similar to The parameters LogFileName and LogFilePrefix cannot be given together.. + /// + internal static string PrefixAndNameProvidedError { + get { + return ResourceManager.GetString("PrefixAndNameProvidedError", resourceCulture); + } + } } } diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.resx b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.resx index a176fb3154..8182657a80 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.resx +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/Resources.resx @@ -123,4 +123,7 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.cs.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.cs.xlf index e2c74fb485..9c35d137af 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.cs.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.cs.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.de.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.de.xlf index e619713ab4..e4e5775c07 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.de.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.de.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.es.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.es.xlf index e30f3238c0..1a20c91052 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.es.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.es.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.fr.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.fr.xlf index 77bccce168..dbd55eaf4a 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.fr.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.fr.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.it.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.it.xlf index e83fa1c5f4..ae97c425a4 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.it.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.it.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ja.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ja.xlf index b3b8dc2b79..21a82ea585 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ja.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ja.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ko.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ko.xlf index 1dbd75ab78..9d0392a0fe 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ko.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ko.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pl.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pl.xlf index 883494b43d..47523fea29 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pl.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pl.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pt-BR.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pt-BR.xlf index 80661f59cd..06e655db74 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pt-BR.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.pt-BR.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ru.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ru.xlf index 5b5d8d6dd8..da9d2b16d3 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ru.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.ru.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.tr.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.tr.xlf index 91e49c75ca..34166be4b2 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.tr.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.tr.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.xlf index 5d613ff35f..7ac0207fec 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.xlf @@ -12,6 +12,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hans.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hans.xlf index abf17ca586..3eb50ce456 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hans.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hans.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hant.xlf b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hant.xlf index 3f148c56dd..d14245ee45 100644 --- a/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hant.xlf +++ b/src/Microsoft.TestPlatform.Extensions.HtmlLogger/Resources/xlf/Resources.zh-Hant.xlf @@ -30,6 +30,11 @@ Html Logger Error : {0} + + The parameters LogFileName and LogFilePrefix cannot be given together. + The parameters LogFileName and LogFilePrefix cannot be given together. + + \ No newline at end of file diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs index f553556a24..121db656c1 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs @@ -489,12 +489,15 @@ private void DeriveTrxFilePath() { if (!string.IsNullOrWhiteSpace(logFilePrefixValue)) { - var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework] ?? string.Empty; - framework = NuGetFramework.Parse(framework).GetShortFolderName(); + var framework = this.parametersDictionary[DefaultLoggerParameterNames.TargetFramework]; + if (framework != null) + { + framework = NuGetFramework.Parse(framework).GetShortFolderName(); + logFilePrefixValue = logFilePrefixValue + "_" + framework; + } - logFilePrefixValue = logFilePrefixValue.Replace(".trx", string.Empty) + "_" + framework + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + this.trxFileExtension; + logFilePrefixValue = logFilePrefixValue + DateTime.Now.ToString("_yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + this.trxFileExtension; this.trxFilePath = Path.Combine(this.testResultsDirPath, logFilePrefixValue); - return; } } diff --git a/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs index 9590a45617..3933ae0774 100644 --- a/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.HtmlLogger.UnitTests/HtmlLoggerTests.cs @@ -118,7 +118,7 @@ public void TestMessageHandlerShouldAddMessageWhenItIsInformation() [TestMethod] public void TestMessageHandlerShouldNotInitializelistForInformationErrorAndWarningMessages() { - Assert.AreEqual(this.htmlLogger.TestRunDetails.RunLevelMessageInformational,null); + Assert.AreEqual(this.htmlLogger.TestRunDetails.RunLevelMessageInformational, null); Assert.AreEqual(this.htmlLogger.TestRunDetails.RunLevelMessageErrorAndWarning, null); } @@ -162,7 +162,7 @@ public void TestResultHandlerShouldKeepTrackOfTotalResult() { var passTestCase1 = CreateTestCase("Pass1"); var passResult1 = new ObjectModel.TestResult(passTestCase1) { Outcome = TestOutcome.Passed }; - + this.htmlLogger.TestResultHandler(new object(), new Mock(passResult1).Object); Assert.AreEqual(this.htmlLogger.TotalTests, 1, "Total Tests"); @@ -171,7 +171,7 @@ public void TestResultHandlerShouldKeepTrackOfTotalResult() [TestMethod] public void TestResultHandlerShouldKeepTrackOfPassedResult() { - var passTestCase2 = CreateTestCase("Pass2"); + var passTestCase2 = CreateTestCase("Pass2"); var passResult2 = new ObjectModel.TestResult(passTestCase2) { Outcome = TestOutcome.Passed }; this.htmlLogger.TestResultHandler(new object(), new Mock(passResult2).Object); @@ -182,7 +182,7 @@ public void TestResultHandlerShouldKeepTrackOfPassedResult() [TestMethod] public void TestResultHandlerShouldKeepTrackOfSkippedResult() { - + var skipTestCase1 = CreateTestCase("Skip1"); var skipResult1 = new ObjectModel.TestResult(skipTestCase1) { Outcome = TestOutcome.Skipped }; @@ -202,7 +202,7 @@ public void TestResultHandlerShouldSetDisplayNameIfDisplayNameIsNull() TestCase = { FullyQualifiedName = "abc" } }; - this.htmlLogger.TestResultHandler(new object(),new Mock(passTestResultExpected).Object); + this.htmlLogger.TestResultHandler(new object(), new Mock(passTestResultExpected).Object); Assert.AreEqual("abc", this.htmlLogger.TestRunDetails.ResultCollectionList.First().ResultList.First().DisplayName); } @@ -265,7 +265,7 @@ public void GetFormattedDurationStringShouldGiveCorrectFormat() TimeSpan ts3 = new TimeSpan(0, 0, 1, 0, 1); Assert.AreEqual(htmlLogger.GetFormattedDurationString(ts3), "1m"); -; + ; TimeSpan ts4 = new TimeSpan(0, 1, 0, 2, 3); Assert.AreEqual(htmlLogger.GetFormattedDurationString(ts4), "1h"); @@ -380,7 +380,7 @@ public void TestCompleteHandlerShouldKeepTackOfSummary() var passResult2 = new ObjectModel.TestResult(passTestCase2) { Outcome = TestOutcome.Passed }; var failResult1 = new ObjectModel.TestResult(failTestCase1) { Outcome = TestOutcome.Failed }; var skipResult1 = new ObjectModel.TestResult(skipTestCase1) { Outcome = TestOutcome.Skipped }; - + this.htmlLogger.TestResultHandler(new object(), new Mock(passResult1).Object); this.htmlLogger.TestResultHandler(new object(), new Mock(passResult2).Object); this.htmlLogger.TestResultHandler(new object(), new Mock(failResult1).Object); @@ -401,7 +401,7 @@ public void TestCompleteHandlerShouldKeepTackOfSummary() } [TestMethod] - public void TestCompleteHandlerShouldCreateCustumHtmlFileNameIfParameterDirectoryIsNull() + public void TestCompleteHandlerShouldCreateCustumHtmlFileNamewithLogFileNameKey() { var parameters = new Dictionary(); parameters[HtmlLoggerConstants.LogFileNameKey] = null; @@ -417,6 +417,91 @@ public void TestCompleteHandlerShouldCreateCustumHtmlFileNameIfParameterDirector Assert.IsTrue(this.htmlLogger.HtmlFilePath.Contains("TestResult")); } + [TestMethod] + public void TestCompleteHandlerShouldCreateCustumHtmlFileNameWithLogPrefix() + { + var parameters = new Dictionary(); + parameters[HtmlLoggerConstants.LogFilePrefixKey] = "sample"; + parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa"; + parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1"; + + var testCase1 = CreateTestCase("TestCase1"); + var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed }; + var resultEventArg1 = new Mock(result1); + this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object); + + this.htmlLogger.Initialize(new Mock().Object, parameters); + this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero)); + Assert.IsFalse(this.htmlLogger.HtmlFilePath.Contains("__")); + } + + [TestMethod] + public void TestCompleteHandlerShouldCreateCustumHtmlFileNameWithLogPrefixIfTargetFrameworkIsNull() + { + var parameters = new Dictionary(); + parameters[HtmlLoggerConstants.LogFilePrefixKey] = "sample"; + parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa"; + parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1"; + + var testCase1 = CreateTestCase("TestCase1"); + var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed }; + var resultEventArg1 = new Mock(result1); + this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object); + + this.htmlLogger.Initialize(new Mock().Object, parameters); + this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero)); + Assert.IsTrue(this.htmlLogger.HtmlFilePath.Contains("sample_net451")); + } + + [TestMethod] + public void TestCompleteHandlerShouldCreateCustumHtmlFileNameWithLogPrefixNull() + { + var parameters = new Dictionary(); + parameters[HtmlLoggerConstants.LogFilePrefixKey] = null; + parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa"; + parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1"; + + var testCase1 = CreateTestCase("TestCase1"); + var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed }; + var resultEventArg1 = new Mock(result1); + + this.mockFileHelper.Setup(x => x.GetStream(It.IsAny(), FileMode.Create, FileAccess.ReadWrite)).Callback((x, y, z) => + { + }).Returns(new Mock().Object); + + this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object); + this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero)); + + this.mockFileHelper.Verify(x => x.GetStream(It.IsAny(), FileMode.Create, FileAccess.ReadWrite), Times.Once); + } + + [TestMethod] + public void TestCompleteHandlerShouldThrowExceptionWithLogPrefixIfTargetFrameworkKeyIsNotPresent() + { + var parameters = new Dictionary(); + parameters[HtmlLoggerConstants.LogFilePrefixKey] = "sample.html"; + parameters[DefaultLoggerParameterNames.TestRunDirectory] = "dsa"; + var testCase1 = CreateTestCase("TestCase1"); + var result1 = new ObjectModel.TestResult(testCase1) { Outcome = TestOutcome.Failed }; + var resultEventArg1 = new Mock(result1); + this.htmlLogger.TestResultHandler(new object(), resultEventArg1.Object); + + this.htmlLogger.Initialize(new Mock().Object, parameters); + + Assert.ThrowsException(() => this.htmlLogger.TestRunCompleteHandler(new object(), new TestRunCompleteEventArgs(null, false, true, null, null, TimeSpan.Zero))); + } + + [TestMethod] + public void IntializeShouldThrowExceptionIfBothPrefixAndNameProvided() + { + this.parameters[HtmlLoggerConstants.LogFileNameKey] = "results.html"; + var trxPrefix = Path.Combine(Path.GetTempPath(), "results"); + this.parameters[HtmlLoggerConstants.LogFilePrefixKey] = "HtmlPrefix"; + this.parameters[DefaultLoggerParameterNames.TargetFramework] = ".NETFramework,Version=4.5.1"; + + Assert.ThrowsException(() => this.htmlLogger.Initialize(events.Object, this.parameters)); + } + [TestMethod] public void TestCompleteHandlerShouldCreateFileCorrectly() { @@ -498,5 +583,5 @@ private static TestCase CreateTestCase(string testCaseName) - + diff --git a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs index fa8fd8907e..a4a1affa9d 100644 --- a/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs +++ b/test/Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests/TrxLoggerTests.cs @@ -712,7 +712,6 @@ private void ValidateDateTimeInTrx(string trxFileName) [TestMethod] [DataRow("results")] - [DataRow("results.trx")] public void CustomTrxFileNameShouldBeConstructedFromRelativeLogFilePrefixParameter(string prefixName) { this.parameters.Remove(TrxLoggerConstants.LogFileNameKey);