Я пытаюсь запустить robolectric внутри AOSP
мой файл 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 -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.
Тест не работает Я не понимаю Что не так Не могли бы вы помочь мне с запуском robolectric для AOSP?
После расследования я выяснил, что Robolectric не работает с тестом.
В исходной документации Google Нашел ссылку на ридми:
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.
Запуск одного тестового класса
$ 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, например это:
Не забудьте про пакет "src" внутри пакета "robotests".
После запуска тестов я получаю вывод:
============================================
[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/
Я надеюсь, что это поможет кому-то. Удачного кодирования!