From bad3e209e7e5162a90961d63e2ca5a6b5547646c Mon Sep 17 00:00:00 2001 From: Brad Stoney Date: Thu, 12 Apr 2018 14:08:21 +0800 Subject: [PATCH] Dynamic data display name (#373) * Corrected documentation * Added dynamic display name to DynamicDataAttribute * Changes requested from PR * Updated the exception message and added a test for public accessibility --- .../Attributes/DynamicDataAttribute.cs | 44 ++- .../MSTest.Core/Interfaces/ITestDataSource.cs | 2 +- .../Resources/FrameworkMessages.Designer.cs | 9 + .../Resources/FrameworkMessages.resx | 3 + .../Resources/xlf/FrameworkMessages.cs.xlf | 5 + .../Resources/xlf/FrameworkMessages.de.xlf | 5 + .../Resources/xlf/FrameworkMessages.es.xlf | 5 + .../Resources/xlf/FrameworkMessages.fr.xlf | 5 + .../Resources/xlf/FrameworkMessages.it.xlf | 5 + .../Resources/xlf/FrameworkMessages.ja.xlf | 5 + .../Resources/xlf/FrameworkMessages.ko.xlf | 5 + .../Resources/xlf/FrameworkMessages.pl.xlf | 5 + .../Resources/xlf/FrameworkMessages.pt-BR.xlf | 5 + .../Resources/xlf/FrameworkMessages.ru.xlf | 5 + .../Resources/xlf/FrameworkMessages.tr.xlf | 5 + .../Resources/xlf/FrameworkMessages.xlf | 5 + .../xlf/FrameworkMessages.zh-Hans.xlf | 5 + .../xlf/FrameworkMessages.zh-Hant.xlf | 5 + .../Attributes/DataRowAttributeTests.cs | 15 + .../Attributes/DynamicDataAttributeTests.cs | 269 +++++++++++++++++- 20 files changed, 406 insertions(+), 6 deletions(-) diff --git a/src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs b/src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs index 881926d7aa..3970fd8728 100644 --- a/src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs +++ b/src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs @@ -56,7 +56,7 @@ public DynamicDataAttribute(string dynamicDataSourceName, DynamicDataSourceType /// Initializes a new instance of the class. /// /// - /// The type in which the test data is declared (as property or in method). + /// The name of method or property having test data. /// /// /// The declaring type of property or method having data. @@ -70,6 +70,16 @@ public DynamicDataAttribute(string dynamicDataSourceName, Type dynamicDataDeclar this.dynamicDataDeclaringType = dynamicDataDeclaringType; } + /// + /// Gets or sets the name of method used to customize the display name in test results. + /// + public string DynamicDataDisplayName { get; set; } + + /// + /// Gets or sets the declaring type used to customize the display name in test results. + /// + public Type DynamicDataDisplayNameDeclaringType { get; set; } + /// public IEnumerable GetData(MethodInfo methodInfo) { @@ -131,7 +141,35 @@ public IEnumerable GetData(MethodInfo methodInfo) /// public string GetDisplayName(MethodInfo methodInfo, object[] data) { - if (data != null) + if (this.DynamicDataDisplayName != null) + { + var dynamicDisplayNameDeclaringType = this.DynamicDataDisplayNameDeclaringType ?? methodInfo.DeclaringType; + + var method = dynamicDisplayNameDeclaringType.GetTypeInfo().GetDeclaredMethod(this.DynamicDataDisplayName); + if (method == null) + { + throw new ArgumentNullException(string.Format("{0} {1}", DynamicDataSourceType.Method, this.DynamicDataDisplayName)); + } + + var parameters = method.GetParameters(); + if (parameters.Length != 2 || + parameters[0].ParameterType != typeof(MethodInfo) || + parameters[1].ParameterType != typeof(object[]) || + method.ReturnType != typeof(string) || + !method.IsStatic || + !method.IsPublic) + { + throw new ArgumentNullException( + string.Format( + FrameworkMessages.DynamicDataDisplayName, + this.DynamicDataDisplayName, + typeof(string).Name, + string.Join(", ", typeof(MethodInfo).Name, typeof(object[]).Name))); + } + + return method.Invoke(null, new object[] { methodInfo, data }) as string; + } + else if (data != null) { return string.Format(CultureInfo.CurrentCulture, FrameworkMessages.DataDrivenResultDisplayName, methodInfo.Name, string.Join(",", data.AsEnumerable())); } @@ -139,4 +177,4 @@ public string GetDisplayName(MethodInfo methodInfo, object[] data) return null; } } -} +} \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Interfaces/ITestDataSource.cs b/src/TestFramework/MSTest.Core/Interfaces/ITestDataSource.cs index bfdc6677b1..d4dada30e1 100644 --- a/src/TestFramework/MSTest.Core/Interfaces/ITestDataSource.cs +++ b/src/TestFramework/MSTest.Core/Interfaces/ITestDataSource.cs @@ -26,7 +26,7 @@ public interface ITestDataSource /// Gets the display name corresponding to test data row for displaying in TestResults. /// /// - /// The method Info of test method. + /// The method info of test method. /// /// /// The test data which is passed to test method. diff --git a/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.Designer.cs b/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.Designer.cs index 5eec0b3402..c6be5aa38c 100644 --- a/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.Designer.cs +++ b/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.Designer.cs @@ -259,6 +259,15 @@ internal static string DoNotUseAssertEquals { } } + /// + /// Looks up a localized string similar to Method {0} must match the expected signature: public static {1} {0}({2}).. + /// + internal static string DynamicDataDisplayName { + get { + return ResourceManager.GetString("DynamicDataDisplayName", resourceCulture); + } + } + /// /// Looks up a localized string similar to Property or method {0} on {1} does not return IEnumerable<object[]>.. /// diff --git a/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.resx b/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.resx index f69c5d8766..4cccff1036 100644 --- a/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.resx +++ b/src/TestFramework/MSTest.Core/Resources/FrameworkMessages.resx @@ -283,4 +283,7 @@ Stack Trace: {4} Value returned by property or method {0} shouldn't be null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.cs.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.cs.xlf index d19ff7898c..666861e7d9 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.cs.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.cs.xlf @@ -282,6 +282,11 @@ Trasování zásobníku: {4} Hodnota vrácená vlastností nebo metodou {0} by neměla být null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.de.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.de.xlf index 56143ba88f..d9d81e4db5 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.de.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.de.xlf @@ -282,6 +282,11 @@ Stapelüberwachung: {4} Der von der Eigenschaft oder Methode "{0}" zurückgegebene Wert darf nicht NULL sein. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.es.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.es.xlf index f930a0153a..91137f070d 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.es.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.es.xlf @@ -282,6 +282,11 @@ Seguimiento de la pila: {4} El valor devuelto por la propiedad o el método {0} no debe ser nulo. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.fr.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.fr.xlf index 985da2644c..c0c3a4d4c7 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.fr.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.fr.xlf @@ -282,6 +282,11 @@ Trace de la pile : {4} La valeur retournée par la propriété ou la méthode {0} ne doit pas être Null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.it.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.it.xlf index f84c704f55..9f5213e405 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.it.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.it.xlf @@ -282,6 +282,11 @@ Analisi dello stack: {4} Il valore restituito dalla proprietà o dal metodo {0} non deve essere Null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ja.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ja.xlf index ecd04a8983..ab8bd050ed 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ja.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ja.xlf @@ -282,6 +282,11 @@ Stack Trace: {4} プロパティやメソッド {0} によって返される値を null にすることはできません。 + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ko.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ko.xlf index 41480009b8..e484b8d7d7 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ko.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ko.xlf @@ -282,6 +282,11 @@ Stack Trace: {4} 속성 또는 메서드 {0}에 의해 반환된 값은 null일 수 없습니다. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pl.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pl.xlf index 1621004d88..430d67d49c 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pl.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pl.xlf @@ -282,6 +282,11 @@ Komunikat o wyjątku: {3} Wartość zwracana przez właściwość lub metodę {0} nie powinna być równa null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pt-BR.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pt-BR.xlf index 7f2fbf489e..3e30042d5c 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pt-BR.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.pt-BR.xlf @@ -282,6 +282,11 @@ Rastreamento de Pilha: {4} O valor retornado pela propriedade ou o método {0} não deve ser nulo null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ru.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ru.xlf index f12238f68d..4e1c5ca046 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ru.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ru.xlf @@ -282,6 +282,11 @@ Stack Trace: {4} Значение, возвращаемое свойством или методом {0}, не должно быть равно NULL. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.tr.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.tr.xlf index e210ca8560..bffa7fd574 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.tr.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.tr.xlf @@ -282,6 +282,11 @@ Yığın İzleme: {4} {0} özelliği veya metodu tarafından döndürülen değer null olamaz. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.xlf index b7d65b6d79..c63df15577 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.xlf @@ -282,6 +282,11 @@ Stack Trace: {4} Value returned by property or method {0} shouldn't be null. + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hans.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hans.xlf index ce161ed75d..5e7f425cc4 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hans.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hans.xlf @@ -282,6 +282,11 @@ Stack Trace: {4} 属性或方法 {0} 返回的值不能为 null。 + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hant.xlf b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hant.xlf index c69481763a..928e4ccc0c 100644 --- a/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hant.xlf +++ b/src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.zh-Hant.xlf @@ -282,6 +282,11 @@ Stack Trace: {4} 屬性或方法 {0} 傳回的值不可為 null。 + + Method {0} must match the expected signature: public static {1} {0}({2}). + Method {0} must match the expected signature: {1} {0}({2}). + + \ No newline at end of file diff --git a/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs b/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs index e57b5d8a11..095c0c91a0 100644 --- a/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs +++ b/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DataRowAttributeTests.cs @@ -81,5 +81,20 @@ public void GetDisplayNameShouldReturnAppropriateName() displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data2); Assert.AreEqual("DataRowTestMethod (First,,Second)", displayName); } + + [TestMethod] + public void GetDisplayNameShouldReturnSpecifiedDisplayName() + { + var dataRowAttribute = new DataRowAttribute(null); + dataRowAttribute.DisplayName = "DataRowTestWithDisplayName"; + + this.dummyTestClass = new DummyTestClass(); + this.testMethodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("DataRowTestMethod"); + + var data = new string[] { "First", "Second", null }; + + var displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data); + Assert.AreEqual("DataRowTestWithDisplayName", displayName); + } } } diff --git a/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DynamicDataAttributeTests.cs b/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DynamicDataAttributeTests.cs index 15db54a4f3..164ef58119 100644 --- a/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DynamicDataAttributeTests.cs +++ b/test/UnitTests/MSTest.Core.Unit.Tests/Attributes/DynamicDataAttributeTests.cs @@ -60,7 +60,7 @@ public void GetDataShouldReadDataFromProperty() public void GetDataShouldReadDataFromPropertyInDifferntClass() { var methodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1"); - this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty", typeof(DummyTestClass)); + this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty2", typeof(DummyTestClass2)); var data = this.dynamicDataAttribute.GetData(methodInfo); Assert.IsTrue(data is IEnumerable); Assert.IsTrue(data.ToList().Count == 2); @@ -80,7 +80,7 @@ public void GetDataShouldReadDataFromMethod() public void GetDataShouldReadDataFromMethodInDifferentClass() { var methodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod2"); - this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod", typeof(DummyTestClass), DynamicDataSourceType.Method); + this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod2", typeof(DummyTestClass2), DynamicDataSourceType.Method); var data = this.dynamicDataAttribute.GetData(methodInfo); Assert.IsTrue(data is IEnumerable); Assert.IsTrue(data.ToList().Count == 2); @@ -121,6 +121,125 @@ public void GetDisplayNameShouldReturnDisplayName() Assert.AreEqual("TestMethod1 (1,2,3)", displayName); } + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldReturnDisplayNameWithDynamicDataDisplayName() + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetCustomDynamicDataDisplayName"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + Assert.AreEqual("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters", displayName); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldReturnDisplayNameWithDynamicDataDisplayNameInDifferentClass() + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetCustomDynamicDataDisplayName2"; + this.dynamicDataAttribute.DynamicDataDisplayNameDeclaringType = typeof(DummyTestClass2); + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + Assert.AreEqual("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters", displayName); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodMissingParameters() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithMissingParameters"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidReturnType() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidReturnType"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidFirstParameterType() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidFirstParameterType"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodInvalidSecondParameterType() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameWithInvalidSecondParameterType"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodNonStatic() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNameNonStatic"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithDynamicDataDisplayNameMethodPrivate() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "GetDynamicDataDisplayNamePrivate"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + + [TestFrameworkV1.TestMethod] + public void GetDisplayNameShouldThrowExceptionWithMissingDynamicDataDisplayNameMethod() + { + Action action = () => + { + var data = new object[] { 1, 2, 3 }; + + this.dynamicDataAttribute.DynamicDataDisplayName = "MissingCustomDynamicDataDisplayName"; + var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); + }; + + ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); + } + [TestFrameworkV1.TestMethod] public void GetDisplayNameShouldReturnEmptyStringIfDataIsNull() { @@ -195,6 +314,93 @@ public static IEnumerable ReusableTestDataMethod() return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } }; } + /// + /// The custom display name method. + /// + /// + /// The method info of test method. + /// + /// + /// The test data which is passed to test method. + /// + /// + /// The . + /// + public static string GetCustomDynamicDataDisplayName(MethodInfo methodInfo, object[] data) + { + return string.Format("DynamicDataTestWithDisplayName {0} with {1} parameters", methodInfo.Name, data.Length); + } + + /// + /// Custom display name method with missing parameters. + /// + /// + /// The . + /// + public static string GetDynamicDataDisplayNameWithMissingParameters() + { + throw new InvalidOperationException(); + } + + /// + /// Custom display name method with invalid return type. + /// + public static void GetDynamicDataDisplayNameWithInvalidReturnType() + { + throw new InvalidOperationException(); + } + + /// + /// Custom display name method with invalid first parameter type. + /// + /// + /// The method info of test method. + /// + /// + /// The test data which is passed to test method. + /// + /// + /// The . + /// + public static string GetDynamicDataDisplayNameWithInvalidFirstParameterType(string methodInfo, object[] data) + { + throw new InvalidOperationException(); + } + + /// + /// Custom display name method with invalid second parameter. + /// + /// + /// The method info of test method. + /// + /// + /// The test data which is passed to test method. + /// + /// + /// The . + /// + public static string GetDynamicDataDisplayNameWithInvalidSecondParameterType(MethodInfo methodInfo, string data) + { + throw new InvalidOperationException(); + } + + /// + /// Custom display name method that is not static. + /// + /// + /// The method info of test method. + /// + /// + /// The test data which is passed to test method. + /// + /// + /// The . + /// + public string GetDynamicDataDisplayNameNonStatic(MethodInfo methodInfo, object[] data) + { + throw new InvalidOperationException(); + } + /// /// The test method 1. /// @@ -241,5 +447,64 @@ public void TestMethod4() public void DataRowTestMethod() { } + + /// + /// Custom display name method that is private. + /// + /// + /// The method info of test method. + /// + /// + /// The test data which is passed to test method. + /// + /// + /// The . + /// + private static string GetDynamicDataDisplayNamePrivate(MethodInfo methodInfo, object[] data) + { + throw new InvalidOperationException(); + } + } + + public class DummyTestClass2 + { + /// + /// Gets the reusable test data property. + /// + public static IEnumerable ReusableTestDataProperty2 + { + get + { + return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } }; + } + } + + /// + /// The reusable test data method. + /// + /// + /// The . + /// + public static IEnumerable ReusableTestDataMethod2() + { + return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } }; + } + + /// + /// The custom display name method. + /// + /// + /// The method info of test method. + /// + /// + /// The test data which is passed to test method. + /// + /// + /// The . + /// + public static string GetCustomDynamicDataDisplayName2(MethodInfo methodInfo, object[] data) + { + return string.Format("DynamicDataTestWithDisplayName {0} with {1} parameters", methodInfo.Name, data.Length); + } } }