React Native Detox Andriod环境搭建(Genymotion版)

添加Android

建立

1.执行“入门指南”中描述的初始设置

  • 需要配置初始化环境 参考这篇文章  入门

2.将detox Android项目添加为androidTestCompile依赖项

  • 以下代码添加到您的android/settings.gradle
include ':detox'
project(':detox').projectDir = new File(rootProject.projectDir, '../node_modules/detox/android/detox')

如果您的项目的RN版本至少为0.46.0,请将oldOkhttp配置字符串更改为newOkhttp,在app/build.gradle此处。

  • android/app/build.gradle需要添加dependencies section(依赖项部分):
androidTestCompile(project(path: ":detox", configuration: "newOkhttpDebug"), {
    exclude group: 'com.android.support', module: 'support-annotations'
})
  •  请注意,minSdkVersion需要至少18。

3. jacoco-coverage在buildscript中添加依赖项

  • 您需要将此添加android/build.gradlebuildscript > dependencies
classpath 'com.palantir:jacoco-coverage:0.4.0'
  • 在同一个文件中,您需要在以下位置添加allprojects > repositories
maven {
    url "https://maven.google.com"
}

 

4.介绍Espresso测试工具

  • Detox 是标准的Android集成测试,  内部使用Espresso,使用AndroidJUnitRunner作为测试运行器(或其子类)。为此你需要修改你的android/app/build.gradle
android {
    defaultConfig {
        testBuildType System.getProperty('testBuildType', 'debug') //this will later be used to control the test apk build type
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

 

5.创建Android Test类

  • 自己创建java文件DetoxTest.java 您需要添加文件android/app/src/androidTest/java/com/[your.package]/DetoxTest.java期望您需要将包更改为您的项目名称。例如此处我的为 /Users/guokai/Desktop/guokai/android/app/src/androidTest/java/com/guokai/DetoxTest.java

package com.example;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.wix.detox.Detox;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by simonracz on 28/05/2017.
 */

@RunWith(AndroidJUnit4.class)
@LargeTest
public class DetoxTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, false, false);

    @Test
    public void runDetoxTests() throws InterruptedException {
        Detox.runTests(mActivityRule);
    }
}

6.添加Android配置

  • 将此部分添加到您的package.json
"detox": {
    "configurations": {
        "android.emu.debug": {
            "binaryPath": "android/app/build/outputs/apk/app-debug.apk",
            "build": "pushd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && popd",
            "type": "android.attached",     //  attached连接真机跟Genymotion
            "name": "192.168.56.101:5555"   //  Genymotion的ip 如果真机的话用 adb devices查出来的名字
        },
        "android.emu.release": {
           "binaryPath": "android/app/build/outputs/apk/app-debug.apk",
           "build": "pushd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release && popd",
           "type": "android.emulator",     //  emulator 连接Avd
           "name": "Nexus_5X_API_25"       //  
        } 
    }
}
  • 注意 DtestBuildType,设置为debugrelease根据主apk类型。
  • 以下设备类型可用于控制Android设备:
  • android.emulatorname例如,提供了启动库存SDK模拟器Nexus_5X_API_25。启动后连接到它。
  • android.attached。连接到已经附加的Android设备。该设备应列在adb devices提供的命令输出中name。使用此类型连接到Genymotion模拟器。

7.运行测试

  • 使用android.emu.debug上面的配置,您可以以标准方式调用它。
detox test -c android.emu.debug

故障排除

  • 如果你得到类似这样的错误:
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
  • 将以下代码添加到android你的部分android/app/build.gradle 
packagingOptions {
    exclude 'META-INF/LICENSE'
}
  • 最后运行完成就是这样的 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值