diff options
author | Brett Chabot <brettchabot@google.com> | 2014-03-26 18:54:27 -0700 |
---|---|---|
committer | Brett Chabot <brettchabot@google.com> | 2014-03-27 12:29:19 -0700 |
commit | 33e00c95cd88b9e71639f03bb2d648824d4b563a (patch) | |
tree | c6b9ce32ddc494f5d2c3cea4775c1e9758a9508f | |
parent | 2399765bd04b20d25d2ef52b821c13789cd3c981 (diff) | |
download | testing-33e00c95cd88b9e71639f03bb2d648824d4b563a.tar.gz |
Skip empty JUnit3 test cases when class path scanning.
Change-Id: Ibc7310b3c52a7f41e3ccff84ac1956d80dd30760
3 files changed, 84 insertions, 2 deletions
diff --git a/support/src/android/support/test/internal/runner/TestLoader.java b/support/src/android/support/test/internal/runner/TestLoader.java index 66b0bd3..9fffc63 100644 --- a/support/src/android/support/test/internal/runner/TestLoader.java +++ b/support/src/android/support/test/internal/runner/TestLoader.java @@ -140,6 +140,11 @@ public class TestLoader { } // TODO: try to find upstream junit calls to replace these checks if (junit.framework.Test.class.isAssignableFrom(loadedClass)) { + // ensure that if a TestCase, it has at least one test method otherwise + // TestSuite will throw error + if (junit.framework.TestCase.class.isAssignableFrom(loadedClass)) { + return hasJUnit3TestMethod(loadedClass); + } return true; } // TODO: look for a 'suite' method? @@ -171,6 +176,26 @@ public class TestLoader { } } + private boolean hasJUnit3TestMethod(Class<?> loadedClass) { + for (Method testMethod : loadedClass.getMethods()) { + if (isPublicTestMethod(testMethod)) { + return true; + } + } + return false; + } + + // copied from junit.framework.TestSuite + private boolean isPublicTestMethod(Method m) { + return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); + } + + // copied from junit.framework.TestSuite + private boolean isTestMethod(Method m) { + return m.getParameterTypes().length == 0 && m.getName().startsWith("test") + && m.getReturnType().equals(Void.TYPE); + } + /** * Utility method for logging debug messages. Only actually logs a message if LOG_TAG is marked * as loggable to limit log spam during normal use. diff --git a/support/tests/src/android/support/test/MyEmptyTest.java b/support/tests/src/android/support/test/MyEmptyTest.java new file mode 100644 index 0000000..4370ff9 --- /dev/null +++ b/support/tests/src/android/support/test/MyEmptyTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.support.test; + +import android.util.Log; + +import junit.framework.TestCase; + +/** + * Placeholder test to verify empty TestCase's are skipped + */ +public class MyEmptyTest extends TestCase { + + public MyEmptyTest() { + Log.i("empty", "I'm here"); + } + + public void foo() {} +} diff --git a/support/tests/src/android/support/test/internal/runner/TestLoaderTest.java b/support/tests/src/android/support/test/internal/runner/TestLoaderTest.java index dbe0dd1..53f9df4 100644 --- a/support/tests/src/android/support/test/internal/runner/TestLoaderTest.java +++ b/support/tests/src/android/support/test/internal/runner/TestLoaderTest.java @@ -15,8 +15,6 @@ */ package android.support.test.internal.runner; -import android.support.test.internal.runner.TestLoader; - import junit.framework.Assert; import junit.framework.TestCase; @@ -34,9 +32,18 @@ import java.io.PrintStream; public class TestLoaderTest { public static class JUnit3Test extends TestCase { + public void testFoo() {} + } + + public static class EmptyJUnit3Test extends TestCase { } + public static abstract class AbstractTest extends TestCase { + public void testFoo() {} + } + + public static class SubClassAbstractTest extends AbstractTest { } public static class JUnit4Test { @@ -45,6 +52,9 @@ public class TestLoaderTest { } } + public static class SubClassJUnit4Test extends JUnit4Test { + } + @RunWith(value = Parameterized.class) public static class JUnit4RunTest { public void thisIsMayBeATest() { @@ -69,6 +79,11 @@ public class TestLoaderTest { } @Test + public void testLoadTests_emptyjunit3() { + Assert.assertNull(mLoader.loadIfTest(EmptyJUnit3Test.class.getName())); + } + + @Test public void testLoadTests_junit4() { assertLoadTestSuccess(JUnit4Test.class); } @@ -105,4 +120,14 @@ public class TestLoaderTest { Assert.assertEquals(0, mLoader.getLoadedClasses().size()); Assert.assertEquals(0, mLoader.getLoadFailures().size()); } + + @Test + public void testLoadTests_junit4SubclassAbstract() { + assertLoadTestSuccess(SubClassJUnit4Test.class); + } + + @Test + public void testLoadTests_junit3SubclassAbstract() { + assertLoadTestSuccess(SubClassAbstractTest.class); + } } |