-
Notifications
You must be signed in to change notification settings - Fork 263
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
Prettify Test Method Display Name #644
Conversation
Friendly "Display Name" for test method names "Method_names_should_be_readable_when_hinted" => "Method names should be readable when hinted"
TestMethod.cs(47,1): error SA1028: Code must not contain trailing whitespace TestMethod.cs(52,1): error SA1028: Code must not contain trailing whitespace TestMethod.cs(111,1): error SA1028: Code must not contain trailing whitespace TestMethod.cs(42,32): error SA1101: Prefix local calls with this TestMethod.cs(112,16): error SA1400: Element 'GetFriendlyName' must declare an access modifier
TestMethod.cs(42,32): error SA1101: Prefix local calls with this
There´s also been other specs #515 |
I love this idea. I was thinking about this same topic.
[TestMethod("Now I can read my test names like a human not a computer 😜")]
public void Blah1()
{
} |
It can be done via two steps
[TestMethod("Consecutive authentication attempts with invalid credentials should fail")]
public void MyTest()
{ ... }
//or
[MyAppTestMethod]
public void Consecutive_authentication_attempts_with_invalid_credentials_should_fail()
{ ... }
//Given the following definitions
public class TestMethodAttrubte : Attribute
{
public string DisplayName { get; set; }
public virtual string GetDisplayName(System.Reflection.MethodInfo methodInfo) =>
string.IsNullOrWhiteSpace(this.DisplayName)
? methodInfo.Name
: this.DisplayName?.Trim();
...
}
public class MyAppsTestMethodAttrubte : TestMethodAttrubte
{
public override string GetDisplayName(MethodInfo methodInfo) =>
methodInfo
.Name
.Replace("_", " ")
.Replace(".", ", ");
} |
I like the general idea, and the extension point. But looking at the xunit spec, and the amount of work and iteration they did on it, I am inclined to keep the implementation at just that. No built in automatic rewrites. That way you can provide your own implementation, which can be simple enough to cover just the conventions you use. This would get the change merged quicker because we will not have to provide a good generic implementation, and multiple configuration options to cater every possible edge case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've seen all 5 commits and reviewed the 'files changed".
In my opinion, it's good but I don't know If I need an specific role in order to Approve this Pull Request.
@nohwnd apologies for the newbie question but : is there any formal procedure (documentation) that I should follow in order to Approve this PR or should I just hit the button? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't found any Unit Test for this change.
It could be added something like this:
[TestClass]
public class TestMethodTest
{
[TestMethod]
public void TestMethodTestConstructorWithTestNameWithUnderscoreShouldReturnReadbleDisplayName()
{
MSTest.TestAdapter.ObjectModel.TestMethod testMethod = new MSTest.TestAdapter.ObjectModel.TestMethod("Dummy_Test_Name", "dummyClassName", "dummyAssemblyName", false);
Assert.AreEqual("Dummy Test Name", testMethod.DisplayName);
}
}
Java JUnit 5 Display Name Generators #interesting 🤔 |
@spottedmahn could you elaborate a bit more on why you find that interesting? 🙂 as I read it it is basically the same concept as here, or am I missing something important? |
Hi guys cc: @spottedmahn I plan to close this PR. I prefer sending in one that satisfies the below use case. What do you think as the following partially implemented this. [TestMethod("Consecutive authentication attempts with invalid credentials should fail")]
public void MyTest()
{ ... }
//or
[MyAppTestMethod]
public void Consecutive_authentication_attempts_with_invalid_credentials_should_fail()
{ ... }
//Given the following definitions
public class TestMethodAttrubte : Attribute
{
public string DisplayName { get; set; }
public virtual string GetDisplayName(System.Reflection.MethodInfo methodInfo) =>
string.IsNullOrWhiteSpace(this.DisplayName)
? methodInfo.Name
: this.DisplayName.Trim();
...
}
public class MyAppsTestMethodAttrubte : TestMethodAttrubte
{
public override string GetDisplayName(MethodInfo methodInfo) =>
methodInfo
.Name
.Replace("_", " ")
.Replace(".", ", ");
}
public class MyHappyAppsTestMethodAttrubte : MyAppsTestMethodAttrubte
{
public override string GetDisplayName(MethodInfo methodInfo) =>
base.GetDisplayName(methodInfo)
.Replace("fails", "😢")
.Replace("should succeed", "😅");
} |
Yep, seems like it. It was interesting to me that another "team" came to the same "discovery" independently. There might also be some ideas in their approach we could learn from |
|
Found a better extension point that could be baked into The below is what i´m currently using cheers. public class PrettyTestClassAttribute : TestClassAttribute
{
public override TestMethodAttribute GetTestMethodAttribute(TestMethodAttribute wrappedTestMethodAttribute)
{
var attribute = base.GetTestMethodAttribute(wrappedTestMethodAttribute);
return attribute is PrettyTestMethodAttribute
? attribute
: 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;
if (wrappedTestMethodAttribute is null)
results = base.Execute(testMethod);
else
results = 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_", "🐞");
return results;
}
} Hopefully will create a new pull request suggesting to bake this into |
Friendly "Display Name" for test method names
similar to
Complements #466
This spec can be improved for other cases PascalCase, CamelCase.
and specified DisplayName should be favoured