-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow Overriding the Test Name #410
Comments
Is the code that decides what to display in Visual Studio in this repo? I would imagine not. I would imagine, it is part the of Visual Studio code base? This looks promising: Microsoft.VisualStudio.TestWindow.Interfaces |
Example: Test name from From testx repo
vs
which one is easier for you to read? 😜 |
@spottedmahn : This repo has the code for the test framework and decides the name that will be decided in VS. The ask is valid and we will look into it. we are open for contributions as well. |
That’s awesome! Where can I read more about how to test such a change? Let’s say I track down the code and change it. How do I point Visual Studio at my new custom assemblies? |
nvm, I think I found it (now that I'm a full pc and not my phone).
|
Making progress!! Branch w/ changes: spottedmahn/testfx/tree/friendly-test-names |
A few issues (and I'm sure there will be more)
internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string source)
{
var isAsync = (testCase.GetPropertyValue(Constants.AsyncTestProperty) as bool?) ?? false;
var testClassName = testCase.GetPropertyValue(Constants.TestClassNameProperty) as string;
// method name from fully qualified name, feels hacky
var parts = testCase.FullyQualifiedName.Split('.');
var methodName = parts[parts.Length - 1];
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?
};
return testElement;
} |
|
Great 🎉!
Hmm... not 100% what you are referring to. I'm going to assume you're referring to:
When I run a test from the Test Explorer window, it is unable to find the test:
The problem |
Hi @jayaranigarg - any updates? |
@spottedmahn : Apologies for the delay.
This exception is generated from here. This effectively means there is some discrepancy in testMethod.Name I believe we will have to fix DisplayName in TestCase at the time of creation of testcase object here. If you need further help debugging the issue, consider raising a PR on your clone of repo and share that PR with us? |
Hi @jayaranigarg - PR submitted... sorry it took so long... |
implemented via #466 |
Found a better and simpler extension point that could be baked into This is what i´m currently using: cheers. public class PrettyTestClassAttribute : TestClassAttribute
{
public override TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute wrappedTestMethodAttribute)
{
var attribute = base.GetTestMethodAttribute(wrappedTestMethodAttribute);
return attribute as PrettyTestTestMethodAttribute ?? new PrettyTestTestMethodAttribute(attribute);
}
}
public class PrettyTestMethodAttribute : TestMethodAttribute
{
private readonly TestMethodAttribute wrappedTestMethodAttribute;
public PrettyTestMethodAttribute(){ }
public PrettyTestMethodAttribute(TestMethodAttribute wrappedTestMethodAttribute) =>
this.wrappedTestMethodAttribute = wrappedTestMethodAttribute;
public override TestResult[] Execute(ITestMethod testMethod)
{
TestResult[] results = testMethodAttribute is null
? base.Execute(testMethod)
: testMethodAttribute.Execute(testMethod);
if(results.Any())
results[0].DisplayName = testMethod.TestMethodName
.Replace("_eq_", " == ")
.Replace("_neq_", " != ")
.Replace("_gt_", " > ")
.Replace("_gte_", " >= ")
.Replace("_lt_", " < ")
.Replace("_lte_", " <= ")
.Replace("Bug_", "🐞")
.Replace("_", " ");
return results;
}
} Hopefully will create a new pull request suggesting to bake this into |
Hi @ahmedalejo - nice! Please share a link to the new PR once created, thanks! |
@ahmedalejo Fixed the code above to actually compile and space after bug. That approach is genius. using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
namespace UnitTestProject84
{
[PrettyTestClass]
public class UnitTest1
{
[TestMethod]
public void Bug_Test_method_1()
{
}
}
public class PrettyTestClassAttribute : TestClassAttribute
{
public override TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute wrappedTestMethodAttribute)
{
var attribute = base.GetTestMethodAttribute(wrappedTestMethodAttribute);
return attribute as PrettyTestMethodAttribute ?? new PrettyTestMethodAttribute(attribute);
}
}
public class PrettyTestMethodAttribute : TestMethodAttribute
{
private readonly TestMethodAttribute wrappedTestMethodAttribute;
public PrettyTestMethodAttribute() { }
public PrettyTestMethodAttribute(TestMethodAttribute wrappedTestMethodAttribute) =>
this.wrappedTestMethodAttribute = wrappedTestMethodAttribute;
public override TestResult[] Execute(ITestMethod testMethod)
{
TestResult[] results = wrappedTestMethodAttribute is null
? base.Execute(testMethod)
: wrappedTestMethodAttribute.Execute(testMethod);
if (results.Any())
results[0].DisplayName = testMethod.TestMethodName
.Replace("_eq_", " == ")
.Replace("_neq_", " != ")
.Replace("_gt_", " > ")
.Replace("_gte_", " >= ")
.Replace("_lt_", " < ")
.Replace("_lte_", " <= ")
.Replace("Bug_", "🐞 ")
.Replace("_", " ");
return results;
}
}
} |
Nice, I just discovered this! One observation: the parameter description on the constructor here is incorrect:
It was probably copied from Ignore and hasn't been changed correctly. |
@mdpopescu was fixed in #715 |
Description
It would be very handy if we could control the
TestName
so we could create nice human-readable test names that show up in the test explorer.Test frameworks like Mocha support this 😊. One example.
Reference: SO Post
Expected behavior
or
Actual behavior
The method name is the test name 😪.
The text was updated successfully, but these errors were encountered: