Skip to content

Commit c45f131

Browse files
Boris SazonovChromium LUCI CQ
Boris Sazonov
authored and
Chromium LUCI CQ
committed
[Android] Support DisabledTest annotation in Robolectric tests
Adds support for @DisabledTest annotation into BaseRobolectricTestRunner, so now DisabledTest works for both instrumentation and Robolectric tests. Bug: 1343791 Change-Id: I4b57ba97fd73288e846f53d7a4d44cd4a4aedee9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3869013 Reviewed-by: James Lee <[email protected]> Auto-Submit: Boris Sazonov <[email protected]> Commit-Queue: Boris Sazonov <[email protected]> Reviewed-by: Andrew Grieve <[email protected]> Cr-Commit-Position: refs/heads/main@{#1043465}
1 parent a385b8e commit c45f131

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

base/BUILD.gn

+3
Original file line numberDiff line numberDiff line change
@@ -4513,6 +4513,7 @@ if (is_android) {
45134513

45144514
deps = [
45154515
":base_java",
4516+
":base_java_test_support",
45164517
"//testing/android/junit:junit_test_support",
45174518
"//third_party/android_support_test_runner:runner_java",
45184519
"//third_party/androidx:androidx_test_core_java",
@@ -4570,6 +4571,8 @@ if (is_android) {
45704571
"test/android/junit/src/org/chromium/base/test/util/CommandLineFlagsNoClassAnnotationCheckTest.java",
45714572
"test/android/junit/src/org/chromium/base/test/util/CommandLineFlagsWithClassAnnotationCheckTest.java",
45724573
"test/android/junit/src/org/chromium/base/test/util/DisableIfTest.java",
4574+
"test/android/junit/src/org/chromium/base/test/util/DisabledTestForClassRobolectricTest.java",
4575+
"test/android/junit/src/org/chromium/base/test/util/DisabledTestRobolectricTest.java",
45734576
"test/android/junit/src/org/chromium/base/test/util/RestrictionSkipCheckTest.java",
45744577
"test/android/junit/src/org/chromium/base/test/util/SkipCheckTest.java",
45754578
]

base/test/android/junit/src/org/chromium/base/test/BaseRobolectricTestRunner.java

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import androidx.test.core.app.ApplicationProvider;
88

9+
import org.junit.runners.model.FrameworkMethod;
910
import org.junit.runners.model.InitializationError;
1011
import org.robolectric.DefaultTestLifecycle;
1112
import org.robolectric.TestLifecycle;
@@ -16,6 +17,7 @@
1617
import org.chromium.base.LifetimeAssert;
1718
import org.chromium.base.PathUtils;
1819
import org.chromium.base.metrics.UmaRecorderHolder;
20+
import org.chromium.base.test.util.DisabledTest;
1921
import org.chromium.testing.local.LocalRobolectricTestRunner;
2022

2123
import java.lang.reflect.Method;
@@ -61,4 +63,13 @@ public BaseRobolectricTestRunner(Class<?> testClass) throws InitializationError
6163
protected Class<? extends TestLifecycle> getTestLifecycleClass() {
6264
return BaseTestLifecycle.class;
6365
}
66+
67+
@Override
68+
protected boolean isIgnored(FrameworkMethod method) {
69+
if (super.isIgnored(method) || method.getAnnotation(DisabledTest.class) != null) {
70+
return true;
71+
}
72+
Class<?> testSuiteClass = method.getDeclaringClass();
73+
return testSuiteClass.getAnnotation(DisabledTest.class) != null;
74+
}
6475
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package org.chromium.base.test.util;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.robolectric.annotation.Config;
11+
12+
import org.chromium.base.test.BaseRobolectricTestRunner;
13+
14+
/** Unit tests for the DisabledTest annotation in Robolectric tests. */
15+
@RunWith(BaseRobolectricTestRunner.class)
16+
@Config(manifest = Config.NONE)
17+
@DisabledTest(message = "This test suite should be disabled")
18+
public class DisabledTestForClassRobolectricTest {
19+
@Test
20+
public void testTestsInDisabledSuitesAreNotExecuted() {
21+
Assert.fail("Tests suites marked with @DisabledTest annotation should not be executed!");
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2022 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package org.chromium.base.test.util;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.robolectric.annotation.Config;
11+
12+
import org.chromium.base.test.BaseRobolectricTestRunner;
13+
14+
/** Unit tests for the DisabledTest annotation in Robolectric tests. */
15+
@RunWith(BaseRobolectricTestRunner.class)
16+
@Config(manifest = Config.NONE)
17+
public class DisabledTestRobolectricTest {
18+
@Test
19+
@DisabledTest(message = "This test should be disabled")
20+
public void testDisabledTestAreNotExecuted() {
21+
Assert.fail("Tests marked with @DisabledTest annotation should not be executed!");
22+
}
23+
}

docs/testing/on_disabling_tests.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ There are a number of different ways to do so:
2626
method name for GTest-based tests, `@unittest.skip` for Python-based tests,
2727
or using the
2828
[DisabledTest](../../base/test/android/javatests/src/org/chromium/base/test/DisabledTest.java)
29-
annotation for JUnit-based Java tests. In these cases, you don't run the
30-
test by default, but you can determine the list of disabled tests at
31-
runtime because the tests are present in the executable, and you may still
32-
be able to force the test to be run via a command-line flag.
29+
annotation for JUnit-based Java tests (this works in both instrumentation
30+
and Robolectric tests). In these cases, you don't run the test by default,
31+
but you can determine the list of disabled tests at runtime because the
32+
tests are present in the executable, and you may still be able to force the
33+
test to be run via a command-line flag.
3334

3435
* Fourth, for test frameworks that support
3536
[expectations files or filter files](https://bit.ly/chromium-test-list-format),

0 commit comments

Comments
 (0)