Skip to content

Commit a3d8b3b

Browse files
Fix that:
1. adb shell am instrument -w -e class com.android.foo.FooTest#testFoo -e notClass com.android.foo.FooTest#testFoo com.android.foo/androidx.test.runner.AndroidJUnitRunner 2. adb shell am instrument -w -e class com.android.foo.FooTest -e notClass com.android.foo.FooTest com.android.foo/androidx.test.runner.AndroidJUnitRunner should perform the same result (no tests run). PiperOrigin-RevId: 599690321
1 parent a76bafd commit a3d8b3b

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

runner/android_junit_runner/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* Attempt to clarify limitations and deprecation reasons in RequiresDevice documentation
1010
* Remove all support for Android SDKs < 19. Minimum is API 19 (Android Kit Kat 4.4)
11+
* Fix that "-e class" and "-e notClass" on the same class/method should perform the same result (no tests run)
1112

1213
**New Features**
1314

runner/android_junit_runner/java/androidx/test/internal/runner/TestRequestBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,6 @@ public Request build() {
811811
Trace.beginSection("build test request");
812812
try {
813813
includedPackages.removeAll(excludedPackages);
814-
includedClasses.removeAll(excludedClasses);
815814
validate(includedClasses);
816815

817816
boolean scanningPath = includedClasses.isEmpty();
@@ -828,6 +827,15 @@ public Request build() {
828827
Log.d(TAG, "Using class path scanning to discover tests");
829828
classNames = getClassNamesFromClassPath();
830829
} else {
830+
// If the set of excludedClasses is equal to the set of includedClasses, no tests should
831+
// run (that's what includeMethods and excludeMethods do currently).
832+
// E.g.,
833+
// 1. adb shell am instrument -w -e class com.android.foo.FooTest#testFoo -e notClass
834+
// com.android.foo.FooTest#testFoo com.android.foo/androidx.test.runner.AndroidJUnitRunner
835+
// 2. adb shell am instrument -w -e class com.android.foo.FooTest -e notClass
836+
// com.android.foo.FooTest com.android.foo/androidx.test.runner.AndroidJUnitRunner
837+
// should perform the same result (no tests run).
838+
includedClasses.removeAll(excludedClasses);
831839
classNames = includedClasses;
832840
}
833841

runner/android_junit_runner/javatests/androidx/test/internal/runner/TestRequestBuilderTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@
6666
import org.junit.Assert;
6767
import org.junit.Before;
6868
import org.junit.Ignore;
69-
import org.junit.Rule;
7069
import org.junit.Test;
71-
import org.junit.rules.ExpectedException;
7270
import org.junit.runner.Description;
7371
import org.junit.runner.JUnitCore;
7472
import org.junit.runner.Request;
@@ -1056,15 +1054,18 @@ public void testParameterizedMethods() throws Exception {
10561054
Assert.assertEquals(3, result.getRunCount());
10571055
}
10581056

1059-
/** Verify adding and removing same class is rejected */
1057+
/** Verify adding a class method and removing same class leaves no tests. */
10601058
@Test
10611059
public void testFilterClassAddMethod() {
1062-
thrown.expect(IllegalArgumentException.class);
1063-
thrown.expectMessage(TestRequestBuilder.MISSING_ARGUMENTS_MSG);
1064-
builder
1065-
.addTestMethod(SampleRunnerFilterSizeTest.class.getName(), "testSmall")
1066-
.removeTestClass(SampleRunnerFilterSizeTest.class.getName())
1067-
.build();
1060+
Request request =
1061+
builder
1062+
.addTestMethod(SampleRunnerFilterSizeTest.class.getName(), "testSmall")
1063+
.removeTestClass(SampleRunnerFilterSizeTest.class.getName())
1064+
.build();
1065+
1066+
JUnitCore testRunner = new JUnitCore();
1067+
Result result = testRunner.run(request);
1068+
Assert.assertEquals(0, result.getRunCount());
10681069
}
10691070

10701071
/** Verify that including and excluding different methods leaves 1 method. */
@@ -1122,12 +1123,15 @@ public void testClassAndNotClass_different() {
11221123
/** Verify that including and excluding the same class leaves no tests. */
11231124
@Test
11241125
public void testClassAndNotClass_same() {
1125-
thrown.expect(IllegalArgumentException.class);
1126-
thrown.expectMessage(TestRequestBuilder.MISSING_ARGUMENTS_MSG);
1127-
builder
1128-
.addTestClass(SampleRunnerFilterSizeTest.class.getName())
1129-
.removeTestClass(SampleRunnerFilterSizeTest.class.getName())
1130-
.build();
1126+
Request request =
1127+
builder
1128+
.addTestClass(SampleRunnerFilterSizeTest.class.getName())
1129+
.removeTestClass(SampleRunnerFilterSizeTest.class.getName())
1130+
.build();
1131+
1132+
JUnitCore testRunner = new JUnitCore();
1133+
Result result = testRunner.run(request);
1134+
Assert.assertEquals(0, result.getRunCount());
11311135
}
11321136

11331137
/** Verify that exclusion filter is filtering out a single test in a class and leaves the rest */
@@ -1275,8 +1279,6 @@ public void testNoApkPath() throws Exception {
12751279
() -> builder.addTestPackage("androidx.test.internal.runner").build());
12761280
}
12771281

1278-
@Rule public ExpectedException thrown = ExpectedException.none();
1279-
12801282
/** Take intersection of test package and class */
12811283
@Test
12821284
public void testBothPackageAndClass() throws Exception {
@@ -1864,6 +1866,7 @@ private void setClassPathScanningResults(String... names) throws IOException {
18641866
private static class RecordingRunListener extends RunListener {
18651867
ArrayList<String> methods = new ArrayList<>();
18661868

1869+
@Override
18671870
public void testFinished(Description description) {
18681871
methods.add(description.getClassName() + "#" + description.getMethodName());
18691872
}

0 commit comments

Comments
 (0)