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

Friendly test names #466

Merged
merged 10 commits into from
Dec 17, 2019
11 changes: 10 additions & 1 deletion src/Adapter/MSTest.CoreAdapter/Discovery/TypeEnumerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery
Expand Down Expand Up @@ -154,6 +154,15 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT
this.type,
warnings);

// get DisplayName from TestMethodAttribute
var myAttribute = method.GetCustomAttributes(false)
.OfType<TestMethodAttribute>()
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
.FirstOrDefault();

var testMethodAttribute = myAttribute as TestMethodAttribute;

testElement.DisplayName = testMethodAttribute?.DisplayName ?? method.Name;

return testElement;
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/Adapter/MSTest.CoreAdapter/Extensions/TestCaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string
var isAsync = (testCase.GetPropertyValue(Constants.AsyncTestProperty) as bool?) ?? false;
var testClassName = testCase.GetPropertyValue(Constants.TestClassNameProperty) as string;

TestMethod testMethod = new TestMethod(testCase.DisplayName, testClassName, source, isAsync);
// method name from fully qualified name, feels hacky
var parts = testCase.FullyQualifiedName.Split('.');
var methodName = parts[parts.Length - 1];
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
TestMethod testMethod = new TestMethod(methodName, testClassName, source, isAsync);

UnitTestElement testElement = new UnitTestElement(testMethod)
{
IsAsync = isAsync,
TestCategory = testCase.GetPropertyValue(Constants.TestCategoryProperty) as string[],
Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int?
};
{
IsAsync = isAsync,
TestCategory = testCase.GetPropertyValue(Constants.TestCategoryProperty) as string[],
Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int?
};

return testElement;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Adapter/MSTest.CoreAdapter/ObjectModel/UnitTestElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public UnitTestElement(TestMethod testMethod)
/// </summary>
public KeyValuePair<string, string>[] DeploymentItems { get; set; }

/// <summary>
/// Gets or sets the DisplayName
/// </summary>
public string DisplayName { get; set; }
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets or sets the compiler generated type name for async test method.
/// </summary>
Expand All @@ -91,6 +96,7 @@ internal TestCase ToTestCase()

TestCase testCase = new TestCase(fullName, TestAdapter.Constants.ExecutorUri, this.TestMethod.AssemblyName);
testCase.DisplayName = this.TestMethod.Name;
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
testCase.DisplayName = this.DisplayName;

testCase.SetPropertyValue(TestAdapter.Constants.TestClassNameProperty, this.TestMethod.FullClassName);

Expand Down
25 changes: 24 additions & 1 deletion src/TestFramework/MSTest.Core/Attributes/VSTestAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;

#pragma warning disable SA1402 // FileMayOnlyContainASingleType
#pragma warning disable SA1649 // SA1649FileNameMustMatchTypeName
Expand Down Expand Up @@ -50,6 +49,30 @@ public virtual TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute te
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class TestMethodAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="TestMethodAttribute"/> class.
/// </summary>
public TestMethodAttribute()
: this(null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TestMethodAttribute"/> class.
/// </summary>
/// <param name="displayName">
/// Message specifies reason for ignoring.
/// </param>
public TestMethodAttribute(string displayName)
{
this.DisplayName = displayName;
}

/// <summary>
/// Gets display Name for the Test Window
/// </summary>
public string DisplayName { get; private set; }

/// <summary>
/// Executes a test method.
/// </summary>
Expand Down