mmdbcn 发表于 2018-1-7 15:47:13

使用Jenkins进行android项目的自动构建(5)

  之前在项目中引入的单元测试使用的是JUnit,可以在构建前进行测试,这里在介绍一下使用Instrumentation 进行单元测试。使用Instrumentation进行测试,比之前多一些步骤,需要把打包apk上传到仓库,然后运行虚拟机,把apk安装到虚拟机中。
  1. Jenkins“外掛程式管理”中,安裝Android Emulator Plugin,然后在“設定系統”->“Android SDK root”中填入SDK的位置,然后新建一个环境变量ANDROID_SDK_HOME,加入android虚拟机的存放位置。
  2. 被测项目的Jenkins设定中,“Goal 及選項” 加入install,用来把打包的apk上传到仓库中。
  3. 建立另一个Jenkins专案,用来构建测试项目,测试项目的签名需要和被测项目一致。
  4. 被测项目的Jenkins设定中增加“建置後動作”,“建置其他專案”,填写测试项目的名称,“Goal 及選項” 加入integration-test或者install以触发测试。install命令会包含执行integration-test,执行integration-test会讲apk上传到仓库并安装到设备,如果没有可运行的设备就会报错。所以如果项目只需要上传apk到仓库,不需要安装到设备的话,请用package install:install(package打包apk,install:install上传到仓库),详细的maven lifecycle请看这里。
  5. 测试项目的pom.xml中加入被测项目的依赖。
  

<dependency>  
<!-- the target apk, which we will test. will automatically be deployed to device in pre-integration-test
  
phase. -->
  
<groupId>com.example.demo</groupId>
  
<artifactId>maven-demo</artifactId>
  
<version>1.0.0</version>
  
<scope>provided</scope>
  
<type>apk</type>
  
</dependency>
  

  
<dependency>
  
<!-- optional: compile time dependency, in this case so that we can read from the R.java for example. -->
  
<groupId>com.example.demo</groupId>
  
<artifactId>maven-demo</artifactId>
  
<version>1.0.0</version>
  
<scope>provided</scope>
  
<type>jar</type>
  
</dependency>
  

  6. 测试项目的Jenkins设定中,“建置環境”中勾选Run an Android emulator during build 并按情况填写,“Goal 及選項” 加入integration-test surefire-report:report用来把测试的apk安装到虚拟机上进行测试。
  7. 构建被测项目,被测项目在完成构建后会自动触发测试项目的构建。
页: [1]
查看完整版本: 使用Jenkins进行android项目的自动构建(5)