Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstable testId for nunit tests #1785

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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