From fa7158c5949de7456609124b77d51252d44557d6 Mon Sep 17 00:00:00 2001 From: Paul Spangler Date: Thu, 11 Oct 2018 12:01:20 -0500 Subject: [PATCH] Allow Task-returning test methods without async keyword Enables asynchronous tests that don't use the async state machine while still rejecting async void methods. --- .../Extensions/MethodInfoExtensions.cs | 4 +- .../Discovery/TestMethodValidatorTests.cs | 18 ++++++- .../Extensions/MethodInfoExtensionsTests.cs | 50 +++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Extensions/MethodInfoExtensions.cs b/src/Adapter/MSTest.CoreAdapter/Extensions/MethodInfoExtensions.cs index f2e30464d3..ca8bc17e51 100644 --- a/src/Adapter/MSTest.CoreAdapter/Extensions/MethodInfoExtensions.cs +++ b/src/Adapter/MSTest.CoreAdapter/Extensions/MethodInfoExtensions.cs @@ -114,8 +114,8 @@ internal static bool HasCorrectTimeout(this MethodInfo method) /// True if the method has a void/task return type.. internal static bool IsVoidOrTaskReturnType(this MethodInfo method) { - return method.GetAsyncTypeName() == null ? ReflectHelper.MatchReturnType(method, typeof(void)) - : ReflectHelper.MatchReturnType(method, typeof(Task)); + return ReflectHelper.MatchReturnType(method, typeof(Task)) + || (ReflectHelper.MatchReturnType(method, typeof(void)) && method.GetAsyncTypeName() == null); } /// diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TestMethodValidatorTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TestMethodValidatorTests.cs index 96761bff69..88fc3be435 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TestMethodValidatorTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/TestMethodValidatorTests.cs @@ -134,7 +134,7 @@ public void IsValidTestMethodShouldReturnFalseForMethodsWithNonVoidReturnType() { this.SetupTestMethod(); var methodInfo = typeof(DummyTestClass).GetMethod( - "MethodWithTaskReturnType", + "MethodWithIntReturnType", BindingFlags.Instance | BindingFlags.Public); Assert.IsFalse(this.testMethodValidator.IsValidTestMethod(methodInfo, typeof(DummyTestClass), this.warnings)); @@ -151,6 +151,17 @@ public void IsValidTestMethodShouldReturnTrueForAsyncMethodsWithTaskReturnType() Assert.IsTrue(this.testMethodValidator.IsValidTestMethod(methodInfo, this.type, this.warnings)); } + [TestMethod] + public void IsValidTestMethodShouldReturnTrueForNonAsyncMethodsWithTaskReturnType() + { + this.SetupTestMethod(); + var methodInfo = typeof(DummyTestClass).GetMethod( + "MethodWithTaskReturnType", + BindingFlags.Instance | BindingFlags.Public); + + Assert.IsTrue(this.testMethodValidator.IsValidTestMethod(methodInfo, this.type, this.warnings)); + } + [TestMethod] public void IsValidTestMethodShouldReturnTrueForMethodsWithVoidReturnType() { @@ -201,6 +212,11 @@ public Task MethodWithTaskReturnType() return Task.Delay(TimeSpan.Zero); } + public int MethodWithIntReturnType() + { + return 0; + } + public void MethodWithVoidReturnType() { } diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/MethodInfoExtensionsTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/MethodInfoExtensionsTests.cs index 4209763668..c9d27f5e42 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/MethodInfoExtensionsTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Extensions/MethodInfoExtensionsTests.cs @@ -74,6 +74,13 @@ public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForAsync Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyInitializeSignature()); } + [TestMethod] + public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForTestMethodsWithoutAsync() + { + var methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticNonAsyncTaskMethodWithTC"); + Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyInitializeSignature()); + } + [TestMethod] public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes() { @@ -127,6 +134,13 @@ public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnTrueForAsyncTes Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyCleanupSignature()); } + [TestMethod] + public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnTrueForTestMethodsWithoutAsync() + { + var methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticNonAsyncTaskMethod"); + Assert.IsTrue(methodInfo.HasCorrectClassOrAssemblyCleanupSignature()); + } + [TestMethod] public void HasCorrectClassOrAssemblyCleanupSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes() { @@ -180,6 +194,13 @@ public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnTrueForAsyncTe Assert.IsTrue(methodInfo.HasCorrectTestInitializeOrCleanupSignature()); } + [TestMethod] + public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnTrueForTestMethodsWithoutAsync() + { + var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod"); + Assert.IsTrue(methodInfo.HasCorrectTestInitializeOrCleanupSignature()); + } + [TestMethod] public void HasCorrectTestInitializeOrCleanupSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes() { @@ -247,6 +268,13 @@ public void HasCorrectTestMethodSignatureShouldReturnTrueForAsyncTestMethods() Assert.IsTrue(methodInfo.HasCorrectTestMethodSignature(false)); } + [TestMethod] + public void HasCorrectTestMethodSignatureShouldReturnTrueForTaskTestMethodsWithoutAsync() + { + var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod"); + Assert.IsTrue(methodInfo.HasCorrectTestMethodSignature(false)); + } + [TestMethod] public void HasCorrectTestMethodSignatureShouldReturnFalseForAsyncTestMethodsWithNonTaskReturnTypes() { @@ -297,6 +325,13 @@ public void IsVoidOrTaskReturnTypeShouldReturnTrueForAsyncTaskMethods() Assert.IsTrue(methodInfo.IsVoidOrTaskReturnType()); } + [TestMethod] + public void IsVoidOrTaskReturnTypeShouldReturnTrueForTaskMethodsWithoutAsync() + { + var methodInfo = typeof(DummyTestClass).GetMethod("PublicNonAsyncTaskMethod"); + Assert.IsTrue(methodInfo.IsVoidOrTaskReturnType()); + } + [TestMethod] public void IsVoidOrTaskReturnTypeShouldReturnFalseForNonVoidMethods() { @@ -453,6 +488,11 @@ public static async Task PublicStaticAsyncTaskMethodWithTC(UTFExtension.TestCont await Task.FromResult(true); } + public static Task PublicStaticNonAsyncTaskMethodWithTC(UTFExtension.TestContext tc) + { + return Task.FromResult(true); + } + public static async void PublicStaticAsyncVoidMethodWithTC(UTFExtension.TestContext tc) { await Task.FromResult(true); @@ -473,6 +513,11 @@ public static async void PublicStaticAsyncVoidMethod() await Task.FromResult(true); } + public static Task PublicStaticNonAsyncTaskMethod() + { + return Task.FromResult(true); + } + public void PublicMethod() { } @@ -502,6 +547,11 @@ public async void PublicAsyncVoidMethod() await Task.FromResult(true); } + public Task PublicNonAsyncTaskMethod() + { + return Task.FromResult(true); + } + [UTF.Timeout(-11)] public void PublicMethodWithInvalidTimeout() {