0

[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 <ljjlee@google.com>
Auto-Submit: Boris Sazonov <bsazonov@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1043465}
This commit is contained in:
Boris Sazonov
2022-09-06 16:01:50 +00:00
committed by Chromium LUCI CQ
parent a385b8e9ae
commit c45f1318e0
5 changed files with 65 additions and 4 deletions

@ -4513,6 +4513,7 @@ if (is_android) {
deps = [
":base_java",
":base_java_test_support",
"//testing/android/junit:junit_test_support",
"//third_party/android_support_test_runner:runner_java",
"//third_party/androidx:androidx_test_core_java",
@ -4570,6 +4571,8 @@ if (is_android) {
"test/android/junit/src/org/chromium/base/test/util/CommandLineFlagsNoClassAnnotationCheckTest.java",
"test/android/junit/src/org/chromium/base/test/util/CommandLineFlagsWithClassAnnotationCheckTest.java",
"test/android/junit/src/org/chromium/base/test/util/DisableIfTest.java",
"test/android/junit/src/org/chromium/base/test/util/DisabledTestForClassRobolectricTest.java",
"test/android/junit/src/org/chromium/base/test/util/DisabledTestRobolectricTest.java",
"test/android/junit/src/org/chromium/base/test/util/RestrictionSkipCheckTest.java",
"test/android/junit/src/org/chromium/base/test/util/SkipCheckTest.java",
]

@ -6,6 +6,7 @@ package org.chromium.base.test;
import androidx.test.core.app.ApplicationProvider;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.robolectric.DefaultTestLifecycle;
import org.robolectric.TestLifecycle;
@ -16,6 +17,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.LifetimeAssert;
import org.chromium.base.PathUtils;
import org.chromium.base.metrics.UmaRecorderHolder;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.testing.local.LocalRobolectricTestRunner;
import java.lang.reflect.Method;
@ -61,4 +63,13 @@ public class BaseRobolectricTestRunner extends LocalRobolectricTestRunner {
protected Class<? extends TestLifecycle> getTestLifecycleClass() {
return BaseTestLifecycle.class;
}
@Override
protected boolean isIgnored(FrameworkMethod method) {
if (super.isIgnored(method) || method.getAnnotation(DisabledTest.class) != null) {
return true;
}
Class<?> testSuiteClass = method.getDeclaringClass();
return testSuiteClass.getAnnotation(DisabledTest.class) != null;
}
}

@ -0,0 +1,23 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.test.util;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
/** Unit tests for the DisabledTest annotation in Robolectric tests. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@DisabledTest(message = "This test suite should be disabled")
public class DisabledTestForClassRobolectricTest {
@Test
public void testTestsInDisabledSuitesAreNotExecuted() {
Assert.fail("Tests suites marked with @DisabledTest annotation should not be executed!");
}
}

@ -0,0 +1,23 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.test.util;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
/** Unit tests for the DisabledTest annotation in Robolectric tests. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class DisabledTestRobolectricTest {
@Test
@DisabledTest(message = "This test should be disabled")
public void testDisabledTestAreNotExecuted() {
Assert.fail("Tests marked with @DisabledTest annotation should not be executed!");
}
}

@ -26,10 +26,11 @@ There are a number of different ways to do so:
method name for GTest-based tests, `@unittest.skip` for Python-based tests,
or using the
[DisabledTest](../../base/test/android/javatests/src/org/chromium/base/test/DisabledTest.java)
annotation for JUnit-based Java tests. In these cases, you don't run the
test by default, but you can determine the list of disabled tests at
runtime because the tests are present in the executable, and you may still
be able to force the test to be run via a command-line flag.
annotation for JUnit-based Java tests (this works in both instrumentation
and Robolectric tests). In these cases, you don't run the test by default,
but you can determine the list of disabled tests at runtime because the
tests are present in the executable, and you may still be able to force the
test to be run via a command-line flag.
* Fourth, for test frameworks that support
[expectations files or filter files](https://bit.ly/chromium-test-list-format),