Home | History | Annotate | Download | only in 3.4.2
      1 # Rules for running robolectric tests.
      2 #
      3 # Uses the following variables:
      4 #
      5 #   LOCAL_JAVA_LIBRARIES
      6 #   LOCAL_STATIC_JAVA_LIBRARIES
      7 #   LOCAL_ROBOTEST_FAILURE_FATAL
      8 #   LOCAL_ROBOTEST_TIMEOUT
      9 #   LOCAL_TEST_PACKAGE
     10 #   LOCAL_ROBOTEST_FILES
     11 #   ROBOTEST_FAILURE_FATAL
     12 #   ROBOTEST_FILTER
     13 #   ROBOTEST_RUN_INDIVIDUALLY
     14 #
     15 #
     16 # If ROBOTEST_FAILURE_FATAL is set to true then failing tests will cause a
     17 # build failure. Otherwise failures will be logged but ignored by make.
     18 #
     19 # If ROBOTEST_FILTER is set to a regex then only tests matching that pattern
     20 # will be run. This currently only works at the class level.
     21 #
     22 # TODO: Switch to a JUnit runner which can support method-level test
     23 # filtering and use that rather than grep to implement ROBOTEST_FILTER.
     24 #
     25 # If ROBOTEST_RUN_INDIVIDUALLY is set to true, each test class will be run by a
     26 # different JVM, preventing any interaction between different tests. This is
     27 # significantly slower than running all tests within the same JVM, but prevents
     28 # unwanted interactions.
     29 #
     30 # Tests classes are found by looking for *Test.java files in
     31 # LOCAL_PATH recursively.
     32 
     33 ################################################
     34 # General settings, independent of the module. #
     35 ################################################
     36 
     37 ### Used for running tests.
     38 
     39 # Where to find Robolectric.
     40 my_robolectric_script_path := $(call my-dir)
     41 # Explicitly define the jars and their classpath ordering.
     42 include $(my_robolectric_script_path)/classpath_jars.mk
     43 my_robolectric_jars := \
     44     $(addprefix $(my_robolectric_script_path)/,$(my_robolectric_runtime_deps)) \
     45     $(call java-lib-files,junit)
     46 
     47 my_collect_target := $(LOCAL_MODULE)-coverage
     48 my_report_target := $(LOCAL_MODULE)-jacoco
     49 # Whether or not to ignore the result of running the robotests.
     50 # LOCAL_ROBOTEST_FAILURE_FATAL will take precedence over ROBOTEST_FAILURE_FATAL,
     51 # if present.
     52 my_failure_fatal := $(if $(LOCAL_ROBOTEST_FAILURE_FATAL)$(ROBOTEST_FAILURE_FATAL),true,false)
     53 # The timeout for the command. A value of '0' means no timeout. The default is
     54 # 10 minutes.
     55 my_timeout := $(if $(LOCAL_ROBOTEST_TIMEOUT),$(LOCAL_ROBOTEST_TIMEOUT),600)
     56 # Command to filter the list of test classes.
     57 # If not specified, defaults to including all the tests.
     58 my_test_filter_command := $(if $(ROBOTEST_FILTER),grep -E "$(ROBOTEST_FILTER)",cat)
     59 
     60 # The directory containing the sources.
     61 my_instrument_makefile_dir := $(dir $(ALL_MODULES.$(LOCAL_TEST_PACKAGE).MAKEFILE))
     62 my_instrument_source_dirs := $(if $(LOCAL_INSTRUMENT_SOURCE_DIRS),\
     63     $(LOCAL_INSTRUMENT_SOURCE_DIRS),\
     64     $(my_instrument_makefile_dir)src $(my_instrument_makefile_dir)java)
     65 
     66 ##########################
     67 # Used by base_rules.mk. #
     68 ##########################
     69 
     70 LOCAL_MODULE_CLASS := FAKE
     71 # This is actually a phony target that is never built.
     72 LOCAL_BUILT_MODULE_STEM := test.fake
     73 # Since it is not built, it cannot be installed. But we will define our own
     74 # dist files, depending on which of the specific targets is invoked.
     75 LOCAL_UNINSTALLABLE_MODULE := true
     76 # Do not build it for checkbuild or mma
     77 LOCAL_DONT_CHECK_MODULE := true
     78 
     79 include $(BUILD_SYSTEM)/base_rules.mk
     80 
     81 
     82 #############################
     83 # Module specific settings. #
     84 #############################
     85 
     86 ### Used for running tests.
     87 
     88 # The list of test classes. Robolectric requires an explicit list of tests to
     89 # run, which is compiled from the Java files ending in "Test" within the
     90 # directory from which this module is invoked.
     91 ifeq ($(strip $(LOCAL_ROBOTEST_FILES)),)
     92     LOCAL_ROBOTEST_FILES := $(call find-files-in-subdirs,$(LOCAL_PATH)/src,*Test.java,.)
     93 endif
     94 # Convert the paths into package names by removing .java extension and replacing "/" with "."
     95 my_tests := $(subst /,.,$(basename $(LOCAL_ROBOTEST_FILES)))
     96 my_tests := $(sort $(shell echo '$(my_tests)' | tr ' ' '\n' | $(my_test_filter_command)))
     97 # The source jars containing the tests.
     98 my_srcs_jars := \
     99     $(foreach lib, \
    100         $(LOCAL_JAVA_LIBRARIES) $(LOCAL_STATIC_JAVA_LIBRARIES), \
    101         $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/classes-pre-proguard.jar) \
    102     $(foreach lib, \
    103         $(LOCAL_TEST_PACKAGE), \
    104         $(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes-pre-proguard.jar)
    105 # The jars needed to run the tests.
    106 my_jars := \
    107     $(my_robolectric_jars) \
    108     prebuilts/sdk/$(LOCAL_SDK_VERSION)/android.jar \
    109     $(call java-lib-files,junitxml) \
    110     $(my_srcs_jars)
    111 
    112 
    113 
    114 # Run tests.
    115 my_target := $(LOCAL_BUILT_MODULE)
    116 my_filename_stem := test
    117 
    118 android_all_lib_path := $(my_robolectric_script_path)/../android-all
    119 my_robolectric_path := $(intermediates.COMMON)/android-all
    120 android_all_jars := $(call find-files-in-subdirs,$(android_all_lib_path),*.jar,.)
    121 copy_android_all_jars := $(foreach j,$(android_all_jars),\
    122     $(android_all_lib_path)/$(j):$(my_robolectric_path)/$(j))
    123 $(my_robolectric_path): $(call copy-many-files,$(copy_android_all_jars))
    124 $(my_target): $(my_robolectric_path)
    125 
    126 # Setting the DEBUG_ROBOLECTRIC environment variable will print additional logging from
    127 # Robolectric and also make it wait for a debugger to be connected.
    128 # For Android Studio / IntelliJ the debugger can be connected via the "remote" configuration:
    129 #     https://www.jetbrains.com/help/idea/2016.2/run-debug-configuration-remote.html
    130 # From command line the debugger can be connected via
    131 #     jdb -attach localhost:5005
    132 ifdef DEBUG_ROBOLECTRIC
    133     # The arguments to the JVM needed to debug the tests.
    134     # - server: wait for connection rather than connecting to a debugger
    135     # - transport: how to accept debugger connections (sockets)
    136     # - address: the port on which to accept debugger connections
    137     # - timeout: how long (in ms) to wait for a debugger to connect
    138     # - suspend: do not start running any code until the debugger connects
    139     my_java_args := \
    140         -Drobolectric.logging.enabled=true \
    141         -Xdebug -agentlib:jdwp=server=y,transport=dt_socket,address=5005,suspend=y
    142 
    143     # Remove the timeout so Robolectric doesn't get killed while debugging
    144     my_timeout := 0
    145 endif
    146 
    147 include $(my_robolectric_script_path)/robotest-internal.mk
    148 # clean local variables
    149 my_java_args :=
    150 my_target :=
    151 
    152 # Target for running robolectric tests using jacoco
    153 my_target := $(LOCAL_BUILT_MODULE)-coverage
    154 my_filename_stem := coverage
    155 $(my_collect_target): $(my_target)
    156 $(my_target): $(call java-lib-files,jvm-jacoco-agent,true) $(my_robolectric_path)
    157 
    158 my_coverage_dir := $(intermediates)/coverage
    159 my_coverage_file := $(my_coverage_dir)/jacoco.exec
    160 
    161 # List of packages to exclude jacoco from running
    162 my_jacoco_excludes := \
    163     org.robolectric.*:org.mockito.*:org.junit.*:org.objectweb.*:com.thoughtworks.xstream.*
    164 # The Jacoco agent JAR.
    165 my_jacoco_agent_jar := $(call java-lib-files,jvm-jacoco-agent,true)
    166 my_java_args := \
    167     -javaagent:$(my_jacoco_agent_jar)=destfile=$(my_coverage_file),excludes=$(my_jacoco_excludes)
    168 include $(my_robolectric_script_path)/robotest-internal.mk
    169 # Clear temporary variables
    170 my_failure_fatal :=
    171 my_jacoco_agent_jar :=
    172 my_jacoco_excludes :=
    173 my_java_args :=
    174 my_robolectric_jars :=
    175 my_target :=
    176 my_tests :=
    177 my_filename_stem :=
    178 
    179 # Target for generating code coverage reports using jacoco.exec
    180 my_target := $(LOCAL_BUILT_MODULE)-jacoco
    181 $(my_report_target): $(my_target)
    182 
    183 # The JAR file containing the report generation tool.
    184 my_coverage_report_class := com.google.android.jacoco.reporter.ReportGenerator
    185 my_coverage_report_jar := $(call java-lib-files,jvm-jacoco-reporter,true)
    186 my_coverage_srcs_jars := $(my_srcs_jars)
    187 my_coverage_report_dist_file := $(my_report_target)-html.zip
    188 
    189 ## jacoco code coverage reports
    190 include $(my_robolectric_script_path)/report-internal.mk
    191 # Clear temporary variables
    192 my_coverage_dir :=
    193 my_coverage_file :=
    194 my_coverage_report_class :=
    195 my_coverage_report_dist_file :=
    196 my_coverage_report_jar :=
    197 my_coverage_srcs_jars :=
    198 my_robolectric_script_path :=
    199 my_robolectric_path :=
    200 my_srcs_jars :=
    201 my_target :=
    202 
    203 # Clear local variables specific to this build.
    204 LOCAL_ROBOTEST_FAILURE_FATAL :=
    205 LOCAL_ROBOTEST_TIMEOUT :=
    206 LOCAL_ROBOTEST_FILES :=
    207 LOCAL_INSTRUMENT_SOURCE_DIRS :=
    208