Skip to content

Commit

Permalink
Unstable testId for nunit tests (#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishkk authored Oct 5, 2018
1 parent 9d5e0b4 commit 8482205
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,19 @@ private ITestElement GetOrCreateTestElement(Guid executionId, Guid parentExecuti
return testElement;
}

Guid testId = Converter.GetTestId(rockSteadyTestResult.TestCase);
TestCase testCase = rockSteadyTestResult.TestCase;
Guid testId = Converter.GetTestId(testCase);

// Scenario for inner test case when parent test element is not present.
var testName = rockSteadyTestResult.TestCase.DisplayName;
if (parentTestElement == null && !string.IsNullOrEmpty(rockSteadyTestResult.DisplayName))
var testName = testCase.DisplayName;
var adapter = testCase.ExecutorUri.ToString();
if (adapter.Contains(TrxLoggerConstants.MstestAdapterString) &&
parentTestElement == null &&
!string.IsNullOrEmpty(rockSteadyTestResult.DisplayName))
{
// Note: For old mstest adapters hierarchical support was not present. Thus inner result of data driven was identified using test result display name.
// Non null test result display name means its a inner result of data driven/ordered test.
// Changing GUID to keep supporting old mstest adapters.
testId = Guid.NewGuid();
testName = rockSteadyTestResult.DisplayName;
}
Expand All @@ -559,7 +566,7 @@ private ITestElement GetOrCreateTestElement(Guid executionId, Guid parentExecuti
// Create test element
if (testElement == null)
{
testElement = Converter.ToTestElement(testId, executionId, parentExecutionId, testName, testType, rockSteadyTestResult.TestCase);
testElement = Converter.ToTestElement(testId, executionId, parentExecutionId, testName, testType, testCase);
testElements.TryAdd(testId, testElement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ internal static class Constants
/// </summary>
public const string TmiTestIdPropertyIdentifier = "MSTestDiscoverer.TmiTestId";

/// <summary>
/// Mstest adapter string
/// </summary>
public const string MstestAdapterString = "mstestadapter";

/// <summary>
/// Ordered test type guid
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ public void TestResultHandlerShouldAddFlatResultsIfParentTestResultIsNotPresent(
Assert.AreEqual(this.testableTrxLogger.TestResultCount, 2, "TestResultHandler is not creating flat results when parent result is not present.");
}

[TestMethod]
public void TestResultHandlerShouldChangeGuidAndDisplayNameForMsTestResultIfParentNotPresentButTestResultNamePresent()
{
this.ValidateTestIdAndNameInTrx(true);
}

[TestMethod]
public void TestResultHandlerShouldNotChangeGuidAndDisplayNameForNonMsTestResultIfParentNotPresentButTestResultNamePresent()
{
this.ValidateTestIdAndNameInTrx(false);
}

[TestMethod]
public void TestResultHandlerShouldAddHierarchicalResultsIfParentTestResultIsPresent()
{
Expand Down Expand Up @@ -695,6 +707,48 @@ private void ValidateDateTimeInTrx(string trxFileName)
}
}

private void ValidateTestIdAndNameInTrx(bool isMstestAdapter)
{
ObjectModel.TestCase testCase = CreateTestCase("TestCase");
testCase.ExecutorUri = isMstestAdapter ? new Uri("some://mstestadapteruri") : new Uri("some://uri");

ObjectModel.TestResult result = new ObjectModel.TestResult(testCase);
result.SetPropertyValue<Guid>(TrxLoggerConstants.ExecutionIdProperty, Guid.NewGuid());
if (isMstestAdapter)
{
result.DisplayName = "testDisplayName";
}

Mock<TestResultEventArgs> resultEventArg = new Mock<TestResultEventArgs>(result);
this.testableTrxLogger.TestResultHandler(new object(), resultEventArg.Object);
var testRunCompleteEventArgs = TrxLoggerTests.CreateTestRunCompleteEventArgs();
this.testableTrxLogger.TestRunCompleteHandler(new object(), testRunCompleteEventArgs);

this.ValidateResultAttributesInTrx(this.testableTrxLogger.trxFile, testCase.Id, testCase.DisplayName, isMstestAdapter);
}

private void ValidateResultAttributesInTrx(string trxFileName, Guid testId, string testName, bool isMstestAdapter)
{
using (FileStream file = File.OpenRead(trxFileName))
{
using (XmlReader reader = XmlReader.Create(file))
{
XDocument document = XDocument.Load(reader);
var resultNode = document.Descendants(document.Root.GetDefaultNamespace() + "UnitTestResult").FirstOrDefault();
if (isMstestAdapter)
{
Assert.AreNotEqual(resultNode.Attributes("testId").FirstOrDefault().Value, testId.ToString());
Assert.AreNotEqual(resultNode.Attributes("testName").FirstOrDefault().Value, testName);
}
else
{
Assert.AreEqual(resultNode.Attributes("testId").FirstOrDefault().Value, testId.ToString());
Assert.AreEqual(resultNode.Attributes("testName").FirstOrDefault().Value, testName);
}
}
}
}

private void ValidateTimeWithinUtcLimits(DateTimeOffset dateTime)
{
Assert.IsTrue(dateTime.UtcDateTime.Subtract(DateTime.UtcNow) < new TimeSpan(0, 0, 0, 60));
Expand Down

0 comments on commit 8482205

Please sign in to comment.