From 6fa88878de6a66b62964996178f83ce1b4dfe1aa Mon Sep 17 00:00:00 2001 From: ZeddzZ Date: Fri, 22 Feb 2019 21:42:08 +0300 Subject: [PATCH 1/2] Added availability to recveive test case data as dictionary --- .../TestCaseData/TestCaseDataTfs.cs | 75 +++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs b/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs index cfb821b..159cdfc 100644 --- a/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs +++ b/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs @@ -1,33 +1,80 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace NUnitTfsTestCase.TestCaseData { public class TestCaseDataTfs { - private static DataTable GetTestCaseParams(int TestCaseId) + /// + /// Takes all parameters from specified test case. + /// Same as , but with casting to dynamic. + /// Saved to avoid errors while users updating from one version to another. + /// + /// Number of test case where parameters taken. + /// IEnumerable with array of values casted to dynamic. + public static IEnumerable GetTestDataInternal(int testCaseId) + { + yield return GetTestDataAsArray(testCaseId) as dynamic; + } + + /// + /// Takes all parameters from specified test case. + /// Params stored as { ParamValue1, ParamValue2, ParamValue3 }. + /// Same as , but without casting to dynamic. + /// + /// Number of test case where parameters taken. + /// IEnumerable with array of values. Each object [] represents single line with parameters. + public static IEnumerable GetTestDataAsArray(int testCaseId) + { + return GetTestData(testCaseId, CollectTestDataAsArray); + } + + /// + /// Takes all parameters from specified test case. + /// Params stored as { { ParamName1, ParamValue1 }, { ParamName2, ParamValue2 }, { ParamName3, ParamValue3 } }. + /// + /// Number of test case where parameters taken. + /// IEnumerable with dictionary of names and values. Each Dictionary represents single line with pairs of name and value of parameter. + public static IEnumerable> GetTestDataAsDictionary(int testCaseId) + { + return GetTestData(testCaseId, CollectTestDataAsDictionary); + } + + private static DataTable GetTestCaseParams(int testCaseId) { - int testCaseId = TestCaseId; var testCase = TfsService.ProjectConnection.TestManagementProject.TestCases.Find(testCaseId); return testCase.DefaultTableReadOnly; } - public static IEnumerable GetTestDataInternal(int TestCaseID) + private static IEnumerable GetTestData(int testCaseId, Func storageFunc) where T: IEnumerable + { + var tcParams = GetTestCaseParams(testCaseId); + foreach (DataRow dr in tcParams.Rows) + { + yield return storageFunc(dr, tcParams.Columns); + } + } + + private static Dictionary CollectTestDataAsDictionary(DataRow dr, DataColumnCollection dcc) + { + var dict = new Dictionary(); + for (int i = 0; i < dcc.Count; i++) + { + dict.Add(dcc[i].ColumnName, dr[i]); + } + return dict; + } + + private static object [] CollectTestDataAsArray(DataRow dr, DataColumnCollection dcc) { - var tb = GetTestCaseParams(TestCaseID); - foreach (DataRow dr in tb.Rows) + var array = new object [dcc.Count]; + for (int i = 0; i < dcc.Count; i++) { - var ob = new List(); - for (var i = 0; i < tb.Columns.Count; i++) - { - ob.Add(dr[i]); - } - yield return ob.ToArray(); + array[i] = dr[i]; } + return array; } } } From 2c0953a0f3297414aaa24d5b5e0abc19a9db5ec6 Mon Sep 17 00:00:00 2001 From: ZeddzZ Date: Fri, 22 Feb 2019 22:13:37 +0300 Subject: [PATCH 2/2] Resolved problems with GetTestDataInternal and yield return --- .../NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs b/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs index 159cdfc..5edde50 100644 --- a/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs +++ b/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs @@ -16,7 +16,7 @@ public class TestCaseDataTfs /// IEnumerable with array of values casted to dynamic. public static IEnumerable GetTestDataInternal(int testCaseId) { - yield return GetTestDataAsArray(testCaseId) as dynamic; + return GetTestDataAsArray(testCaseId); } ///