'How run robolectric inside AOSP

I try run robolectric inside AOSP

my Android.bp file looks like

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",
    ],
}

test looks:

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);
    }
}

I try run with atest

$ atest -c MyRoboTests

But test does not run. Out of console looks:

==================
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.

Test does not work I don't understand What is wrong Could you help me with running robolectric for AOSP?



Solution 1:[1]

After investigation, I have figured out that Robolectric isn't run with atest.

In google source documentation I found the link to readme:

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

Running a single test class

$ croot
$ make RunSettingsRoboTests ROBOTEST_FILTER=<ClassName>

So in my sample I for my Android.bp file

//############################################################
// 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,
    },

}

To create a command I need to add "Run" to the module name "MyRoboTests"

$ make RunMyRoboTests ROBOTEST_FILTER=UsbTest

Note this!

You should correct create folders structure for roblectrics test like this: enter image description here

Do not forget for "src" package inside "robotests" package.

After running tests I get the output:

============================================
[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: 

to open result files:

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

enter image description here

I hope this helps someone. Happy coding!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1