-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathDataTestMethodAttribute.cs
103 lines (87 loc) · 3.3 KB
/
DataTestMethodAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
using System;
using System.Collections.Generic;
#region DataRow
/// <summary>
/// Attribute for data driven test where data can be specified inline.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodAttribute : TestMethodAttribute
{
/// <summary>
/// Find all data rows and execute.
/// </summary>
/// <param name="testMethod">
/// The test Method.
/// </param>
/// <returns>
/// The <see cref="TestResult[]"/>.
/// </returns>
public override TestResult[] Execute(ITestMethod testMethod)
{
DataRowAttribute[] dataRows = testMethod.GetAttributes<DataRowAttribute>(false);
if (dataRows == null || dataRows.Length == 0)
{
return new TestResult[] { new TestResult() { Outcome = UnitTestOutcome.Failed, TestFailureException = new Exception(FrameworkMessages.NoDataRow) } };
}
return RunDataDrivenTest(testMethod, dataRows);
}
/// <summary>
/// Run data driven test method.
/// </summary>
/// <param name="testMethod"> Test method to execute. </param>
/// <param name="dataRows"> Data Row. </param>
/// <returns> Results of execution. </returns>
internal static TestResult[] RunDataDrivenTest(ITestMethod testMethod, DataRowAttribute[] dataRows)
{
List<TestResult> results = new List<TestResult>();
foreach (var dataRow in dataRows)
{
TestResult result = testMethod.Invoke(dataRow.Data);
if (!string.IsNullOrEmpty(dataRow.DisplayName))
{
result.DisplayName = dataRow.DisplayName;
}
results.Add(result);
}
return results.ToArray();
}
}
/// <summary>
/// DataRowattribute for defining inline data for DataTestMethodAttribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class DataRowAttribute : Attribute
{
/// <summary>
/// Constructor without parameters to suppress error for CLS complaint.
/// </summary>
/// <param name="data1"> The data object. </param>
public DataRowAttribute(object data1)
{
this.Data = new object[] { data1 };
}
/// <summary>
/// Can provide any number of arguments.
/// </summary>
/// <param name="data1"> A data object. </param>
/// <param name="moreData"> More data. </param>
public DataRowAttribute(object data1, params object[] moreData)
{
this.Data = new object[moreData.Length + 1];
this.Data[0] = data1;
Array.Copy(moreData, 0, this.Data, 1, moreData.Length);
}
/// <summary>
/// Gets Data for calling test method.
/// </summary>
public object[] Data { get; private set; }
/// <summary>
/// Gets or sets display name in test results for customization.
/// </summary>
public string DisplayName { get; set; }
}
#endregion
}