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

Trx logger class name fix - NUnit data driven tests #1677

Merged
merged 7 commits into from
Jul 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public override TestType TestType
get { return Constants.UnitTestType; }
}

/// <summary>
/// Gets the test method.
/// </summary>
public TestMethod TestMethod
{
get { return this.testMethod; }
}

/// <summary>
/// Gets or sets the storage.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,44 +573,56 @@ private static string GetOwner(ObjectModel.TestCase rockSteadyTestCase)
}

/// <summary>
/// Gets TestMethod for given testCase name and its class name.
/// Gets test class name.
/// </summary>
/// <param name="testDisplayName">test case display name</param>
/// <param name="rockSteadyTestCase">rockSteady Test Case</param>
/// <returns>The <see cref="TestMethod"/></returns>
private static TestMethod GetTestMethod(string testDisplayName, string testCaseName, string source)
/// <param name="testName">Test name.</param>
/// <param name="fullyQualifiedName">Fully qualified name.</param>
/// <param name="source">Source.</param>
/// <returns>Test class name.</returns>
private static string GetTestClassName(string testName, string fullyQualifiedName, string source)
{
string className = "DefaultClassName";
if (testCaseName.Contains("."))
var className = "DefaultClassName";

// In case, fullyQualifiedName ends with testName, className is checked within remaining value of fullyQualifiedName.
// Example: In case, testName = TestMethod1(2, 3, 4.0d) and fullyQualifiedName = TestProject1.Class1.TestMethod1(2, 3, 4.0d), className will be checked within 'TestProject1.Class1.' only
var nameToCheck = fullyQualifiedName.EndsWith(testName) ?
fullyQualifiedName.Substring(0, fullyQualifiedName.Length - testName.Length) :
fullyQualifiedName;

// C# test case scenario.
if (nameToCheck.Contains("."))
{
className = testCaseName.Substring(0, testCaseName.LastIndexOf('.'));
return nameToCheck.Substring(0, nameToCheck.LastIndexOf('.'));
}
else if (testCaseName.Contains("::"))

// C++ test case scenario (we would have a "::" instead of a '.')
if (nameToCheck.Contains("::"))
{
// if this is a C++ test case then we would have a "::" instaed of a '.'
className = testCaseName.Substring(0, testCaseName.LastIndexOf("::"));
className = nameToCheck.Substring(0, nameToCheck.LastIndexOf("::"));

// rename for a consistent behaviour for all tests.
className = className.Replace("::", ".");
return className.Replace("::", ".");
}
else

// Ordered test, web test scenario (Setting class name as source name if FQDn doesnt have . or ::)
try
{
// Setting class name as source name if FQDn doesnt have . or :: [E.g. ordered test, web test]
try
string testCaseSource = Path.GetFileNameWithoutExtension(source);
if (!String.IsNullOrEmpty(testCaseSource))
{
string testCaseSource = Path.GetFileNameWithoutExtension(source);
if (!String.IsNullOrEmpty(testCaseSource))
{
className = testCaseSource;
}
return testCaseSource;
}
catch (ArgumentException)
}
catch (ArgumentException ex)
{
// If source is not valid file path, then className will continue to point default value.
if (ObjectModel.EqtTrace.IsVerboseEnabled)
{
// If source is not valid file path, then className will continue to point default value.
ObjectModel.EqtTrace.Verbose("Converter: GetTestClassName: " + ex);
}
}

return new TestMethod(testDisplayName, className);
return className;
}

/// <summary>
Expand All @@ -635,7 +647,8 @@ private static TestElement CreateTestElement(Guid testId, string name, string fu
else
{
var codeBase = source;
var testMethod = GetTestMethod(name, fullyQualifiedName, source);
var className = GetTestClassName(name, fullyQualifiedName, source);
var testMethod = new TestMethod(name, className);

testElement = new UnitTestElement(testId, name, adapter, testMethod);
(testElement as UnitTestElement).CodeBase = codeBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,41 +636,6 @@ public void GetCustomPropertyValueFromTestCaseShouldReadCategoyrAttributesFromTe
CollectionAssert.AreEqual(listCategoriesExpected, listCategoriesActual);
}

/// <summary>
/// Unit test for assigning or populating test categories read to the unit test element.
/// </summary>
[TestMethod]
public void ToTestElementShouldAssignTestCategoryOfUnitTestElement()
{
ObjectModel.TestCase testCase = CreateTestCase("TestCase1");
ObjectModel.TestResult result = new ObjectModel.TestResult(testCase);
TestProperty testProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject));

testCase.SetPropertyValue(testProperty, new[] { "AsmLevel", "ClassLevel", "MethodLevel" });

var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase);

object[] expected = new[] { "MethodLevel", "ClassLevel", "AsmLevel" };

CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray().OrderByDescending(x => x.ToString()).ToArray());
}

/// <summary>
/// Unit test for regression when there's no test categories.
/// </summary>
[TestMethod]
public void ToTestElementShouldNotFailWhenThereIsNoTestCategoreis()
{
ObjectModel.TestCase testCase = CreateTestCase("TestCase1");
ObjectModel.TestResult result = new ObjectModel.TestResult(testCase);

var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase);

object[] expected = Enumerable.Empty<Object>().ToArray();

CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray());
}

[TestMethod]
public void CRLFCharactersShouldGetRetainedInTrx()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

namespace Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests.Utility
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.TestPlatform.Extensions.TrxLogger.Utility;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ObjectModel;
using System;
using System.Collections.Generic;
using System.IO;
using TestPlatformObjectModel = Microsoft.VisualStudio.TestPlatform.ObjectModel;
using TestOutcome = VisualStudio.TestPlatform.ObjectModel.TestOutcome;
using TrxLoggerConstants = Microsoft.TestPlatform.Extensions.TrxLogger.Utility.Constants;
using TrxLoggerOutcome = Microsoft.TestPlatform.Extensions.TrxLogger.ObjectModel.TestOutcome;
using UriDataAttachment = VisualStudio.TestPlatform.ObjectModel.UriDataAttachment;

Expand Down Expand Up @@ -60,6 +64,77 @@ public void ToCollectionEntriesShouldRenameAttachmentUriIfTheAttachmentNameIsSam
Directory.Delete(tempDir, true);
}

/// <summary>
/// Unit test for assigning or populating test categories read to the unit test element.
/// </summary>
[TestMethod]
public void ToTestElementShouldAssignTestCategoryOfUnitTestElement()
{
TestPlatformObjectModel.TestCase testCase = CreateTestCase("TestCase1");
TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase);
TestProperty testProperty = TestProperty.Register("MSTestDiscoverer.TestCategory", "String array property", string.Empty, string.Empty, typeof(string[]), null, TestPropertyAttributes.Hidden, typeof(TestObject));

testCase.SetPropertyValue(testProperty, new[] { "AsmLevel", "ClassLevel", "MethodLevel" });

var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase);

object[] expected = new[] { "MethodLevel", "ClassLevel", "AsmLevel" };

CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray().OrderByDescending(x => x.ToString()).ToArray());
}

/// <summary>
/// Unit test for regression when there's no test categories.
/// </summary>
[TestMethod]
public void ToTestElementShouldNotFailWhenThereIsNoTestCategoreis()
{
TestPlatformObjectModel.TestCase testCase = CreateTestCase("TestCase1");
TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase);

var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testCase.DisplayName, TrxLoggerConstants.UnitTestType, testCase);

object[] expected = Enumerable.Empty<Object>().ToArray();

CollectionAssert.AreEqual(expected, unitTestElement.TestCategories.ToArray());
}

[TestMethod]
public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnEndsWithTestName()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRY; use datarow here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed dry

{
var expectedClassName = "TestProject1.Class1";
var fullyQualifiedName = expectedClassName + "." + "TestMethod1(2, 3, 4.0d)";
var testName = "TestMethod1(2, 3, 4.0d)";

ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName);
}

[TestMethod]
public void ToTestElementShouldContainExpectedTestMethodPropertiesIfFqnDoesNotEndsWithTestName()
{
var expectedClassName = "TestProject1.Class1.TestMethod1(2, 3, 4";
var fullyQualifiedName = "TestProject1.Class1.TestMethod1(2, 3, 4.0d)";
var testName = "TestMethod1";

ValidateTestMethodProperties(testName, fullyQualifiedName, expectedClassName);
}

private void ValidateTestMethodProperties(string testName, string fullyQualifiedName, string expectedClassName)
{
TestPlatformObjectModel.TestCase testCase = CreateTestCase(fullyQualifiedName);
TestPlatformObjectModel.TestResult result = new TestPlatformObjectModel.TestResult(testCase);

var unitTestElement = Converter.ToTestElement(testCase.Id, Guid.Empty, Guid.Empty, testName, TrxLoggerConstants.UnitTestType, testCase) as UnitTestElement;

Assert.AreEqual(expectedClassName, unitTestElement.TestMethod.ClassName);
Assert.AreEqual(testName, unitTestElement.TestMethod.Name);
}

private static TestCase CreateTestCase(string fullyQualifiedName)
{
return new TestPlatformObjectModel.TestCase(fullyQualifiedName, new Uri("some://uri"), "DummySourceFileName");
}

private static void SetupForToCollectionEntries(out string tempDir, out List<AttachmentSet> attachmentSets, out TestRun testRun,
out string testResultsDirectory)
{
Expand Down