AOSP 中如何 运行 robolectric

How run robolectric inside AOSP

我在 AOSP

中尝试 运行 robolectric

我的 Android.bp 文件看起来像

android_robolectric_test {
    name: "MyRoboTests",

     srcs: [
             "src/**/*.java",
         ],

    static_libs: [
        "SettingsLib-robo-testutils",
        "android-support-annotations",
        "androidx.test.core",
        "androidx.test.runner",
        "androidx.test.ext.junit",
        "androidx.test.espresso.core",
    ],

    java_resource_dirs: ["config", "resources"],
    instrumentation_for: "MyProject",
    test_options: {
        timeout: 36000,
        shards: 10,
    },

    coverage_libs: [
        "Settings-core",
        "SettingsLib",
        "SettingsLib-search",
    ],
}

测试外观:

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class UsbTest {

    @Test
    public void test() {
        Object object = new Object();
        assertNotNull(object);
    }
}

我用atest运行试试

$ atest -c MyRoboTests

但测试没有运行。控制台外的外观:

==================
Notice:
  We collect anonymous usage statistics in accordance with our Content Licenses (https://source.android.com/setup/start/licenses), Contributor License Agreement (https://opensource.google.com/docs/cla/), Privacy Policy (https://policies.google.com/privacy) and Terms of Service (https://policies.google.com/terms).
==================


Finding Tests...
Found 'MyUsbTest' as MODULE


Running Tests...
Run 'atest --history' to review test result history.

测试无效 我不明白 怎么了 你能帮我 运行ning robolectric for AOSP 吗?

经过调查,我发现 Robolectric 不是 运行 运行

In google source documentation 我找到了 link 自述文件:

If the change is submitted to AOSP, then a unit test is required. To get more information about how to write Robolectric based tests, see the readme file packages/apps/Settings/tests/robotests/README.md.

README.md

运行单次测试class

$ croot
$ make RunSettingsRoboTests ROBOTEST_FILTER=<ClassName>

所以在我的示例中,我的 Android.bp 文件

//############################################################
// Settings Robolectric test target.                         #
//############################################################
android_robolectric_test {
    name: "MyRoboTests",

     srcs: [
             "src/**/*.java",
         ],

    static_libs: [
        "SettingsLib-robo-testutils",
        "android-support-annotations",
        "androidx.test.core",
        "androidx.test.runner",
        "androidx.test.ext.junit",
        "androidx.test.espresso.core",
    ],

    java_resource_dirs: ["config", "resources"],
    instrumentation_for: "MyProject",
    test_options: {
        timeout: 36000,
        shards: 10,
    },

}

要创建命令,我需要将“运行”添加到模块名称“MyRoboTests”

$ make RunMyRoboTests ROBOTEST_FILTER=UsbTest

注意这个!

您应该更正 roblectrics 测试的创建文件夹结构,例如 this

不要忘记“robotests”包中的“src”包。

在 运行ning 测试后我得到输出:

============================================
[100% 10/10] build out/target/product/generic_car_x86_64/obj/ROBOLECTRIC/RunMyBoboRoboTests0_intermediates/test.fake
RunMyBoboRoboTests0: 
RunMyBoboRoboTests0: Time: 15.395
RunMyBoboRoboTests0: 
RunMyBoboRoboTests0: OK (12 tests)
RunMyBoboRoboTests0: 

打开结果文件:

$ nautilus out/target/product/generic_car_x86_64/obj/ROBOLECTRIC/RunMyBoboRoboTests0_intermediates/

我希望这对某人有所帮助。编码愉快!