androidandroid-sourceandroid-instrumentationandroid-ctsandroid-tradefederation

How to build Trade Federation test cases


I am able to compile and execute Trade Federation test cases which are located inside /tools/tradefederation/core/tests. But how can I execute the test cases which are located inside my project? My Unit and Instrumentation test cases are located inside /vendor/xyz/packages/apps/MyApp/test folder. How can I build Trade Federation inside this folder and run my test cases? Any help regarding this is appreciated.


Solution

  • Since I couldn't get an answer from others, I did little research and managed to solve it myself. I built my project main classes and test classes into a separate jar file which has the reference to all its dependencies. Then I put it inside out/host/linux-x86/tradefed folder so that tradefed can detect my tests. Then I executed them from the terminal after building tradefed. Below Android.mk file will generate the jar file and copy it to tradefed folder,

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE_TAGS := optional
    
    LOCAL_STATIC_JAVA_LIBRARIES := \
        android-support-v13 \
        android-support-v4 
    
    
    LOCAL_SRC_FILES := $(call all-java-files-under, src)
    LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, res)
    
    LOCAL_PACKAGE_NAME:= MyUTSampleApp
    LOCAL_CERTIFICATE := platform
    LOCAL_DEX_PREOPT := false
    
    
    include $(BUILD_PACKAGE)
    # To include test folder.
    #include $(call all-makefiles-under,$(LOCAL_PATH))
    
    
    include $(CLEAR_VARS)
    LOCAL_SRC_FILES := $(call all-java-files-under, test)
    LOCAL_MODULE := sample-tests
    LOCAL_MODULE_TAGS := optional
    LOCAL_STATIC_JAVA_LIBRARIES := mockito-host junit-host android-support-test MyUTSampleApp android_stubs_current
    #LOCAL_JAVA_LIBRARIES := tradefed host-libprotobuf-java-full 
    
    
    LOCAL_JAR_MANIFEST := MANIFEST.mf
    
    
    include $(BUILD_HOST_JAVA_LIBRARY)
    
    
    # makefile rules to copy jars to HOST_OUT/tradefed
    # so tradefed.sh can automatically add to classpath
    DEST_JAR := $(HOST_OUT)/tradefed/$(LOCAL_MODULE).jar
    $(DEST_JAR): $(LOCAL_BUILT_MODULE)
          $(copy-file-to-new-target)
    $(LOCAL_INSTALLED_MODULE) : $(DEST_JAR)
    

    And you can use another shell script file to execute the test cases like this,

    TEST_CLASS="com.example.myutsampleapp.LocalManagerTest"
    
    FORWARDED_ARGS=()
    while [[ $# -gt 0 ]]; do
      next="$1"
      case ${next} in
      --class)
        TEST_CLASS="$2"
        shift
        ;;
      *)
        FORWARDED_ARGS+=("$1")
        ;;
      esac
      shift
    done
    
    
    /home/bsherif/workspace/source/tools/tradefederation/core/tradefed.sh run singleCommand host -n \
      --console-result-reporter:suppress-passed-tests \
      --class ${TEST_CLASS} ${FORWARDED_ARGS[*]}