./gradlew测试已连接AndroidTest会删除我的getFilesDir()文件夹 [英] ./gradlew test connectedAndroidTest erases my getFilesDir() folder

查看:83
本文介绍了./gradlew测试已连接AndroidTest会删除我的getFilesDir()文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Room数据库写入我的getFilesDir()文件夹.(将数据库写到可移动SD卡上显然需要进行一些主要研究!)

I'm writing a Room database into my getFilesDir() folder. (Writing the database onto the removable SD card is apparently going to require some major research!)

当我手动运行我的应用程序时,我想写一些记录并将其保留在数据库中,因此不需要继续写它们.运行测试时,我将数据库名称从"* .dat"切换为"_test.dat"(以Ruby on Rails或Django的"environments"系统为例).测试可以自由删除记录.

When I manually run my app, I want to write some records and leave them in the database, so I don't need to keep writing them again. When I run my tests, I switch the database name from "*.dat" to "_test.dat" (think Ruby on Rails's or Django's "environments" system). The tests are free to erase records.

当我手动用镊子钳一下Android Studio中的每个测试以运行该系统时,该系统可以工作.但是,当我在 gradlew 中批量运行所有内容时,会擦除数据库的"* .dat"版本.这意味着每次我手动测试时,我都必须不断地手动重新填充数据库.

This system works when I manually tweezer each test in Android Studio to run it. But when I run everything in a batch, in gradlew, something erases the "*.dat" version of the database. This means I must constantly manually repopulate the database, each time I manually test.

gradlew 里面的什么在擦除getFilesDir()文件夹的内容?以及如何(不弄清楚如何使用外部"存储),我会击败它吗?

What inside gradlew is erasing the contents of my getFilesDir() folder? And how (without figuring out how to use "external" storage), do I defeat it?

可根据要求提供代码示例,但这全都是通用的东西.切换到getExternalFilesDir()不能解决问题.据说StackOverflow尝试 ./gradlew test connectedAndroidTest -x uninstallAndroidTest ,但是没有 uninstallAndroidTest 任务.

Code samples available on request, but it's all just generic stuff. Switching to getExternalFilesDir() does not fix the problem. StackOverflow said to try ./gradlew test connectedAndroidTest -x uninstallAndroidTest, but there's no uninstallAndroidTest task.

顶级 build.gradle :

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

tasks.withType(Test) {
  testLogging {
    exceptionFormat "full"
    events "started", "skipped", "passed", "failed"
    showStandardStreams true
  }
}

app/build.gradle :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.allflat.planarinfinity"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
//    packagingOptions {
//        exclude 'META-INF/DEPENDENCIES'
//        exclude 'META-INF/LICENSE'
//        exclude 'META-INF/LICENSE.txt'
//        exclude 'META-INF/license.txt'
//        exclude 'META-INF/NOTICE'
//        exclude 'META-INF/NOTICE.txt'
//        exclude 'META-INF/notice.txt'
//        exclude 'META-INF/ASL2.0'
//        exclude 'META-INF/INDEX.LIST'
//    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
   // testImplementation 'org.mockito:mockito-core:2.19.0'
    testImplementation 'androidx.arch.core:core-testing:2.1.0'
    androidTestImplementation 'org.powermock:powermock-core:2.0.2'
    androidTestImplementation 'org.powermock:powermock-module-junit4:2.0.2'
    androidTestImplementation 'org.powermock:powermock-api-easymock:2.0.2'
    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
//    androidTestImplementation 'androidx.test.platform.app:'
    implementation 'androidx.room:room-runtime:2.2.0'
    annotationProcessor 'androidx.room:room-compiler:2.2.0'
    testImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'org.easymock:easymock:4.0.2'
//    androidTestImplementation 'org.mockito:mockito-core:2.19.0'
}


这是这里的卸载":


It's the 'uninstall' in here:

有人可以拿出来吗?真烦人.

Could someone take it out? It's annoying everyone.

推荐答案

每个获奖作品都在这里...

Per the winning entry here...

运行connectedAndroidTest并跳过卸载

...答案是编写一个包含以下内容的 Rakefile :

...the answer is to write a Rakefile containing:

task default: [ :unit_test_and_install, :espresso_test ]

task :unit_test_and_install do
    sh './gradlew --console=verbose test installDebug installDebugAndroidTest'
end

devices = `adb devices`
serial_numbers = devices.split("\n")[1..-1].map{|q| q.split("\t")[0] }

task :espresso_test do

    threads = serial_numbers.map do |sn|
        Thread.new do
            sh "adb -s #{sn} shell am instrument -w -r -e package com.mycorp.myapp -e disableAnalytics true " + 
                    "com.mycorp.myapp.test/androidx.test.runner.AndroidJUnitRunner | tee #{sn}.txt"
        end
    end

    threads.each &:join

    serial_numbers.each do |sn|
        grop = `grep "^OK .*tests" #{sn}.txt`

        if grop == ''
            sh "cat #{sn}.txt"
            abort 'FAILURES on ' + sn
        end
    end

end

然后只需输入 $ rake .所有测试都可以运行,而我的手动测试数据和配置都可以保留.

Then just enter $ rake. All the tests run, while my manual-testing data and configurations all survive.

我们需要一个Ruby Rakefile ,而不是 Makefile ,因为我们需要处理输出并查找错误,以将0或1成功返回到环境.但是 adb等无法通过测试错误是语法错误"规则,并且在错误时不会传播正确的退出值.我希望我的命令行中止而不提交损坏的代码,因此我们将测试输出存储到SERIALNUMBER.txt文件中,然后 grep .

We need a Ruby Rakefile, not a Makefile, because we need to process the output and look for errors to successfully return either 0 or 1 to the environment. But adb etc fails the rule "test faults are syntax errors," and does not propagate the correct exit value at fault time. I want my command line to abend and not commit broken code, so we stash the test outputs into SERIALNUMBER.txt files and then grep them.

我们需要线程,因此许多设备可以同时进行测试而无需等待对方.

And we need threads, so many devices can test at the same time without waiting for each other.

正确检测错误可以使错误集成到一行中.git commit -am'每次都发表相同的评论'&&git push .如果有任何错误,它们将显示在控制台上,并且不会发生 git commit .

Correctly detecting errors allows one to integrate in one line, $ rake && git commit -am 'the same comment every time' && git push. If there were any errors, they display on the console, and the git commit does not happen.

我们需要 Makefile 样式的处理,因为如果任何命令失败,我们都需要按照测试错误是语法错误"的规则来停止处理.

And we need Makefile-style processing, because if any command fails we need to stop processing, following the rule "test faults are syntax errors."

(Espresso测试故障时出现了一个新错误,我们不再收到显示HTML输出文件在何处的消息,而是因为错误本身在喷出中,所以问题不大.)

(A new bug is at Espresso test fault time we no longer get the message saying where is the HTML output file, but because the error itself is in the spew this is less of a problem.)

另一个错误:如果我有两个平板电脑通过USB调试连接,则会出现错误:多个设备/仿真器,这完全是虚假的,因为 ./gradlew测试已连接AndroidTest 自然可以在所有设备上正常工作.这就是为什么 Rakefile 必须找到所有设备,然后向每个设备分配 adb shell -s 的原因.

Another bug: If I have two tablets hooked up via USB debugging, I get error: more than one device/emulator, which is utterly bogus because ./gradlew test connectedAndroidTest naturally works correctly on all devices... That's why the Rakefile must find all the devices and then dispatch adb shell -s to each one.

请在以下位置写下有关使用低级 Rakefile 调用 gradlew 的所有和所有投诉:[__].

Please write any and all complaints about using a lowly Rakefile to call gradlew here: [__].

这篇关于./gradlew测试已连接AndroidTest会删除我的getFilesDir()文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆