-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathDynamicDataMethodTests.cs
More file actions
183 lines (155 loc) · 7.48 KB
/
DynamicDataMethodTests.cs
File metadata and controls
183 lines (155 loc) · 7.48 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
using Microsoft.Testing.Platform.Helpers;
namespace MSTest.Acceptance.IntegrationTests;
[TestClass]
public sealed class DynamicDataMethodTests : AcceptanceTestBase<DynamicDataMethodTests.TestAssetFixture>
{
[TestMethod]
[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
public async Task DynamicDataTestWithParameterizedDataProviderMethod(string tfm)
{
var testHost = TestHost.LocateFrom(AssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync("--settings my.runsettings", cancellationToken: TestContext.CancellationToken);
testHostResult.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed);
testHostResult.AssertOutputContainsSummary(failed: 3, passed: 9, skipped: 0);
// failed TestMethodSingleParameterIntCountMismatchSmaller (0ms)
// Parameter count mismatch.
// at System.Reflection.MethodBaseInvoker.ThrowTargetParameterCountException()
// at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
// at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
//
// failed TestMethodSingleParameterIntCountMismatchLarger (0ms)
// Parameter count mismatch.
// at System.Reflection.MethodBaseInvoker.ThrowTargetParameterCountException()
// at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
// at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
//
// failed TestMethodParamsNotSupported (0ms)
// Dynamic data method 'TestClass1.GetDataParams' should be static, non-generic, and cannot have 'params' parameter.
testHostResult.AssertOutputMatchesRegex(@"failed TestMethodSingleParameterIntCountMismatchSmaller \(\d+ms\)[\r\n]+\s+Parameter count mismatch.");
testHostResult.AssertOutputMatchesRegex(@"failed TestMethodSingleParameterIntCountMismatchLarger \(\d+ms\)[\r\n]+\s+Parameter count mismatch.");
testHostResult.AssertOutputMatchesRegex(@"failed TestMethodParamsNotSupported \(\d+ms\)[\r\n]+\s+Dynamic data method 'TestClass1.GetDataParams' should be static, non-generic, and cannot have 'params' parameter.");
testHostResult.AssertOutputContains("TestMethodSingleParameterInt called with: 4");
testHostResult.AssertOutputContains("TestMethodSingleParameterInt called with: 5");
testHostResult.AssertOutputContains("TestMethodSingleParameterInt called with: 6");
testHostResult.AssertOutputContains("TestMethodTwoParametersIntAndString called with: 4, Hello1");
testHostResult.AssertOutputContains("TestMethodTwoParametersIntAndString called with: 5, Hello2");
testHostResult.AssertOutputContains("TestMethodTwoParametersIntAndString called with: 6, Hello3");
testHostResult.AssertOutputContains("TestMethodSingleParameterIntArray called with: 5");
testHostResult.AssertOutputContains("TestMethodSingleParameterIntArray called with: 7");
testHostResult.AssertOutputContains("TestMethodSingleParameterIntArray called with: 9");
}
public sealed class TestAssetFixture() : TestAssetFixtureBase()
{
public const string ProjectName = "DynamicDataMethodTests";
public string ProjectPath => GetAssetPath(ProjectName);
public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
private const string SourceCode = """
#file DynamicDataMethodTests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
</ItemGroup>
<ItemGroup>
<None Update="*.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
#file TestClass1.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestClass1
{
[TestMethod]
[DynamicData(nameof(GetDataSingleParameterInt), 4)]
public void TestMethodSingleParameterInt(int a)
{
// We call GetDataSingleParameterInt with 4, so it should yield 4, 5, and 6.
Console.WriteLine($"TestMethodSingleParameterInt called with: {a}");
}
[TestMethod]
[DynamicData(nameof(GetDataTwoParametersIntAndString), 4, "Hello")]
public void TestMethodTwoParametersIntAndString(int a, string s)
{
// We call GetDataTwoParametersIntAndString with 4 and "Hello", so it should yield:
// - (4, "Hello1")
// - (5, "Hello2")
// - (6, "Hello3")
Console.WriteLine($"TestMethodTwoParametersIntAndString called with: {a}, {s}");
}
[TestMethod]
[DynamicData(nameof(GetDataSingleParameterIntArray), [new int[] { 4, 5, 6 }])]
public void TestMethodSingleParameterIntArray(int x)
{
// We call GetDataSingleParameterIntArray with an array (4, 5, and 6), so it should yield 5, 7, and 9.
Console.WriteLine($"TestMethodSingleParameterIntArray called with: {x}");
}
[TestMethod]
[DynamicData(nameof(GetDataSingleParameterInt))]
public void TestMethodSingleParameterIntCountMismatchSmaller(int x)
{
// This test should fail due parameter count mismatch.
}
[TestMethod]
[DynamicData(nameof(GetDataSingleParameterInt), 1, 2)]
public void TestMethodSingleParameterIntCountMismatchLarger(int x)
{
// This test should fail due parameter count mismatch.
}
[TestMethod]
[DynamicData(nameof(GetDataParams), 1, 2)]
public void TestMethodParamsNotSupported(int x)
{
// This test should fail because we don't support params.
}
public static IEnumerable<int> GetDataSingleParameterInt(int i)
{
yield return i++;
yield return i++;
yield return i++;
}
public static IEnumerable<object[]> GetDataSingleParameterIntArray(int[] input)
{
yield return [1 + input[0]];
yield return [2 + input[1]];
yield return [3 + input[2]];
}
public static IEnumerable<object[]> GetDataTwoParametersIntAndString(int i, string s)
{
yield return new object[] { i++, s + "1" };
yield return new object[] { i++, s + "2" };
yield return new object[] { i++, s + "3" };
}
public static IEnumerable<int> GetDataParams(params int[] i)
{
yield return 0;
yield return 1;
}
}
#file my.runsettings
<RunSettings>
<MSTest>
<CaptureTraceOutput>false</CaptureTraceOutput>
</MSTest>
</RunSettings>
""";
}
public TestContext TestContext { get; set; }
}