Skip to content

Commit 0301eea

Browse files
spanglercomayankbansal018
authored andcommitted
Allow Task-returning test methods without async keyword (#510)
Enables asynchronous tests that don't use the async state machine while still rejecting async void methods.
1 parent 3050a15 commit 0301eea

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

src/Adapter/MSTest.CoreAdapter/Extensions/MethodInfoExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ internal static bool HasCorrectTimeout(this MethodInfo method)
114114
/// <returns>True if the method has a void/task return type..</returns>
115115
internal static bool IsVoidOrTaskReturnType(this MethodInfo method)
116116
{
117-
return method.GetAsyncTypeName() == null ? ReflectHelper.MatchReturnType(method, typeof(void))
118-
: ReflectHelper.MatchReturnType(method, typeof(Task));
117+
return ReflectHelper.MatchReturnType(method, typeof(Task))
118+
|| (ReflectHelper.MatchReturnType(method, typeof(void)) && method.GetAsyncTypeName() == null);
119119
}
120120

121121
/// <summary>

test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TestMethodValidatorTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void IsValidTestMethodShouldReturnFalseForMethodsWithNonVoidReturnType()
134134
{
135135
this.SetupTestMethod();
136136
var methodInfo = typeof(DummyTestClass).GetMethod(
137-
"MethodWithTaskReturnType",
137+
"MethodWithIntReturnType",
138138
BindingFlags.Instance | BindingFlags.Public);
139139

140140
Assert.IsFalse(this.testMethodValidator.IsValidTestMethod(methodInfo, typeof(DummyTestClass), this.warnings));
@@ -151,6 +151,17 @@ public void IsValidTestMethodShouldReturnTrueForAsyncMethodsWithTaskReturnType()
151151
Assert.IsTrue(this.testMethodValidator.IsValidTestMethod(methodInfo, this.type, this.warnings));
152152
}
153153

154+
[TestMethod]
155+
public void IsValidTestMethodShouldReturnTrueForNonAsyncMethodsWithTaskReturnType()
156+
{
157+
this.SetupTestMethod();
158+
var methodInfo = typeof(DummyTestClass).GetMethod(
159+
"MethodWithTaskReturnType",
160+
BindingFlags.Instance | BindingFlags.Public);
161+
162+
Assert.IsTrue(this.testMethodValidator.IsValidTestMethod(methodInfo, this.type, this.warnings));
163+
}
164+
154165
[TestMethod]
155166
public void IsValidTestMethodShouldReturnTrueForMethodsWithVoidReturnType()
156167
{
@@ -201,6 +212,11 @@ public Task MethodWithTaskReturnType()
201212
return Task.Delay(TimeSpan.Zero);
202213
}
203214

215+
public int MethodWithIntReturnType()
216+
{
217+
return 0;
218+
}
219+
204220
public void MethodWithVoidReturnType()
205221
{
206222
}

test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/MethodInfoExtensionsTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForAsync
7474
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyInitializeSignature());
7575
}
7676

77+
[TestMethod]
78+
public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForTestMethodsWithoutAsync()
79+
{
80+
var methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticNonAsyncTaskMethodWithTC");
81+
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyInitializeSignature());
82+
}
83+
7784
[TestMethod]
7885
public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
7986
{
@@ -127,6 +134,13 @@ public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnTrueForAsyncTes
127134
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyCleanupSignature());
128135
}
129136

137+
[TestMethod]
138+
public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnTrueForTestMethodsWithoutAsync()
139+
{
140+
var methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticNonAsyncTaskMethod");
141+
Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyCleanupSignature());
142+
}
143+
130144
[TestMethod]
131145
public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
132146
{
@@ -180,6 +194,13 @@ public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnTrueForAsyncTe
180194
Assert.IsTrue(methodInfo.HasCorrectTestInitializeOrCleanupSignature());
181195
}
182196

197+
[TestMethod]
198+
public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnTrueForTestMethodsWithoutAsync()
199+
{
200+
var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod");
201+
Assert.IsTrue(methodInfo.HasCorrectTestInitializeOrCleanupSignature());
202+
}
203+
183204
[TestMethod]
184205
public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
185206
{
@@ -247,6 +268,13 @@ public void HasCorrectTestMethodSignatureShouldReturnTrueForAsyncTestMethods()
247268
Assert.IsTrue(methodInfo.HasCorrectTestMethodSignature(false));
248269
}
249270

271+
[TestMethod]
272+
public void HasCorrectTestMethodSignatureShouldReturnTrueForTaskTestMethodsWithoutAsync()
273+
{
274+
var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod");
275+
Assert.IsTrue(methodInfo.HasCorrectTestMethodSignature(false));
276+
}
277+
250278
[TestMethod]
251279
public void HasCorrectTestMethodSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes()
252280
{
@@ -297,6 +325,13 @@ public void IsVoidOrTaskReturnTypeShouldReturnTrueForAsyncTaskMethods()
297325
Assert.IsTrue(methodInfo.IsVoidOrTaskReturnType());
298326
}
299327

328+
[TestMethod]
329+
public void IsVoidOrTaskReturnTypeShouldReturnTrueForTaskMethodsWithoutAsync()
330+
{
331+
var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod");
332+
Assert.IsTrue(methodInfo.IsVoidOrTaskReturnType());
333+
}
334+
300335
[TestMethod]
301336
public void IsVoidOrTaskReturnTypeShouldReturnFalseForNonVoidMethods()
302337
{
@@ -453,6 +488,11 @@ public static async Task PublicStaticAsyncTaskMethodWithTC(UTFExtension.TestCont
453488
await Task.FromResult(true);
454489
}
455490

491+
public static Task PublicStaticNonAsyncTaskMethodWithTC(UTFExtension.TestContext tc)
492+
{
493+
return Task.FromResult(true);
494+
}
495+
456496
public static async void PublicStaticAsyncVoidMethodWithTC(UTFExtension.TestContext tc)
457497
{
458498
await Task.FromResult(true);
@@ -473,6 +513,11 @@ public static async void PublicStaticAsyncVoidMethod()
473513
await Task.FromResult(true);
474514
}
475515

516+
public static Task PublicStaticNonAsyncTaskMethod()
517+
{
518+
return Task.FromResult(true);
519+
}
520+
476521
public void PublicMethod()
477522
{
478523
}
@@ -502,6 +547,11 @@ public async void PublicAsyncVoidMethod()
502547
await Task.FromResult(true);
503548
}
504549

550+
public Task PublicNonAsyncTaskMethod()
551+
{
552+
return Task.FromResult(true);
553+
}
554+
505555
[UTF.Timeout(-11)]
506556
public void PublicMethodWithInvalidTimeout()
507557
{

0 commit comments

Comments
 (0)