-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMeadowTestMethodAttribute.cs
88 lines (74 loc) · 2.95 KB
/
MeadowTestMethodAttribute.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace Meadow.UnitTestTemplate
{
public class MeadowTestMethodAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
return TestMethodAttributeHelper.Execute(testMethod);
}
}
public class MeadowDataTestMethodAttribute : DataTestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
return TestMethodAttributeHelper.Execute(testMethod);
}
}
class TestMethodAttributeHelper
{
public static TestResult[] Execute(ITestMethod testMethod)
{
// Get our test context
TestContext testContext = testMethod.GetTestContext();
// Get a possible Description attribute
var desc = testMethod.MethodInfo.GetCustomAttribute<DescriptionAttribute>(inherit: true)?.Description;
if (string.IsNullOrEmpty(desc))
{
desc = testMethod.MethodInfo.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>(inherit: true)?.Description;
}
string customDisplayName = null;
// Test has method arguments, include them in the description
if (testMethod.Arguments?.Length > 0)
{
// Test has description, use it as a string formatter for the arguments
if (!string.IsNullOrEmpty(desc))
{
try
{
customDisplayName = testMethod.TestMethodName + " - " + string.Format(CultureInfo.InvariantCulture, desc, testMethod.Arguments);
}
catch (Exception ex)
{
// String format with description failed
customDisplayName = testMethod.TestMethodName + " - " + ex.Message;
}
}
else
{
// No description formatter, only append arguments to test name
customDisplayName = testMethod.TestMethodName + " - " + string.Join(", ", testMethod.Arguments);
}
}
// Test only has description, use in display name
else if (desc != null)
{
customDisplayName = testMethod.TestMethodName + " - " + desc;
}
if (!string.IsNullOrEmpty(customDisplayName))
{
testContext.GetInternalTestState().CustomDisplayName = customDisplayName;
}
var executeResult = testMethod.Invoke(testMethod.Arguments);
if (!string.IsNullOrEmpty(customDisplayName))
{
executeResult.DisplayName = customDisplayName;
}
return new[] { executeResult };
}
}
}