如何使用 Android.mk 在 AOSP Java 和 Robolectric 中获得代码覆盖率
How to get Code Coverage in AOSP Java and Robolectric with Android.mk
我正在用 AOSP 编写应用程序。我需要为此应用程序进行单元测试。为此,我使用了 robolectric。该应用程序和测试是使用 make (Android.mk) 编译的。
我需要一份代码覆盖率报告,但不知道如何获得它。我只找到了使用 gradle 的解决方案,我们在项目中没有使用它。
有人知道如何获得代码覆盖率的解决方案吗?
public class Example extends Service
{
@Override
public void onCreate() {
Log.d("Example"," onCreate");
}
public boolean foo(){
Log.d("Example"," foo");
return true;
}
@Override
public IBinder onBind(Intent intent) {
return new ExampleBinder();
}
public class ExampleBinder extends Binder {
// Returns service object
public Example getService() {
return Example.this;
}
}
}
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := RoboTestExampleImpl
LOCAL_MODULE := RoboTestExampleImpl
LOCAL_MODULE_TAGS := tests
LOCAL_JACK_ENABLED := disabled
LOCAL_SRC_FILES := $(call all-java-files-under, java)
LOCAL_COMPATIBILITY_SUITE := e-tests
LOCAL_INSTRUMENTATION_FOR := RoboTestExample
include $(BUILD_STATIC_JAVA_LIBRARY)
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBinder.class, manifest=Config.NONE)
public class ExampleRoboTest {
private static final String TAG = "ExampleRoboTest";
private ServiceController<Example> controller;
Example service;
@Before
public void setUp() {
controller = Robolectric.buildService(Example.class);
service = controller.create().get();
}
@After
public void tearDown() {
}
@Test
public void foo()
{
assertTrue(service.foo());
}
}
#############################################
# RoboExample Robolectric test target. #
#############################################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := RoboTestExampleTest
LOCAL_MODULE := RoboTestExampleTest
LOCAL_MODULE_TAGS := tests
LOCAL_JACK_ENABLED := disabled
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_SRC_FILES := \
$(call all-java-files-under, src) \
LOCAL_JAVA_LIBRARIES := \
robolectric_android-all-stub \
Robolectric_all-target \
LOCAL_STATIC_JAVA_LIBRARIES := \
jacocoagent \
LOCAL_COMPATIBILITY_SUITE := e-tests
LOCAL_INSTRUMENTATION_FOR := RoboTestExample
include $(BUILD_STATIC_JAVA_LIBRARY)
#############################################################
# runner target to run the previous target. #
#############################################################
include $(CLEAR_VARS)
LOCAL_MODULE := RunRoboTestExampleTest
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_JAVA_LIBRARIES := \
RoboTestExampleTest\
robolectric_android-all-stub \
Robolectric_all-target \
LOCAL_TEST_PACKAGE := RoboTestExample
LOCAL_INSTRUMENT_SOURCE_DIRS := $(dir $(LOCAL_PATH))../main/java
LOCAL_ROBOTEST_TIMEOUT := 36000
include external/robolectric-shadows/run_robotests.mk
$ mma RunRoboTestExampleTest-jacoco
我正在用 AOSP 编写应用程序。我需要为此应用程序进行单元测试。为此,我使用了 robolectric。该应用程序和测试是使用 make (Android.mk) 编译的。 我需要一份代码覆盖率报告,但不知道如何获得它。我只找到了使用 gradle 的解决方案,我们在项目中没有使用它。
有人知道如何获得代码覆盖率的解决方案吗?
public class Example extends Service
{
@Override
public void onCreate() {
Log.d("Example"," onCreate");
}
public boolean foo(){
Log.d("Example"," foo");
return true;
}
@Override
public IBinder onBind(Intent intent) {
return new ExampleBinder();
}
public class ExampleBinder extends Binder {
// Returns service object
public Example getService() {
return Example.this;
}
}
}
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := RoboTestExampleImpl
LOCAL_MODULE := RoboTestExampleImpl
LOCAL_MODULE_TAGS := tests
LOCAL_JACK_ENABLED := disabled
LOCAL_SRC_FILES := $(call all-java-files-under, java)
LOCAL_COMPATIBILITY_SUITE := e-tests
LOCAL_INSTRUMENTATION_FOR := RoboTestExample
include $(BUILD_STATIC_JAVA_LIBRARY)
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBinder.class, manifest=Config.NONE)
public class ExampleRoboTest {
private static final String TAG = "ExampleRoboTest";
private ServiceController<Example> controller;
Example service;
@Before
public void setUp() {
controller = Robolectric.buildService(Example.class);
service = controller.create().get();
}
@After
public void tearDown() {
}
@Test
public void foo()
{
assertTrue(service.foo());
}
}
#############################################
# RoboExample Robolectric test target. #
#############################################
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := RoboTestExampleTest
LOCAL_MODULE := RoboTestExampleTest
LOCAL_MODULE_TAGS := tests
LOCAL_JACK_ENABLED := disabled
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_SRC_FILES := \
$(call all-java-files-under, src) \
LOCAL_JAVA_LIBRARIES := \
robolectric_android-all-stub \
Robolectric_all-target \
LOCAL_STATIC_JAVA_LIBRARIES := \
jacocoagent \
LOCAL_COMPATIBILITY_SUITE := e-tests
LOCAL_INSTRUMENTATION_FOR := RoboTestExample
include $(BUILD_STATIC_JAVA_LIBRARY)
#############################################################
# runner target to run the previous target. #
#############################################################
include $(CLEAR_VARS)
LOCAL_MODULE := RunRoboTestExampleTest
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_JAVA_LIBRARIES := \
RoboTestExampleTest\
robolectric_android-all-stub \
Robolectric_all-target \
LOCAL_TEST_PACKAGE := RoboTestExample
LOCAL_INSTRUMENT_SOURCE_DIRS := $(dir $(LOCAL_PATH))../main/java
LOCAL_ROBOTEST_TIMEOUT := 36000
include external/robolectric-shadows/run_robotests.mk
$ mma RunRoboTestExampleTest-jacoco