Phl*_*lip 6 android-testing gradlew android-espresso
我正在将Room数据库写入getFilesDir()文件夹。(将数据库写到可移动SD卡上显然需要进行一些重大研究!)
当我手动运行我的应用程序时,我想写一些记录并将其保留在数据库中,因此不需要继续写它们。运行测试时,我将数据库名称从“ * .dat”切换为“ _test.dat”(以Ruby on Rails或Django的“ environments”系统为例)。测试可以自由删除记录。
当我手动镊子在Android Studio中运行每个测试时,此系统可以工作。但是,当我批量运行in中的所有内容时,将gradlew删除数据库的“ * .dat”版本。这意味着每次我手动测试时,我都必须不断地手动重新填充数据库。
gradlew擦除getFilesDir()文件夹内容的内部是什么?以及如何(不弄清楚如何使用“外部”存储),我会击败它吗?
代码样例可根据要求提供,但全都是通用的东西。切换到getExternalFilesDir()不能解决问题。说要尝试StackOverflow ./gradlew test connectedAndroidTest -x uninstallAndroidTest,但是没有uninstallAndroidTest任务。
最高层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
}
}
Run Code Online (Sandbox Code Playgroud)
的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'
}
Run Code Online (Sandbox Code Playgroud)
这是这里的“卸载”:
有人可以拿出来吗?真烦人
根据此处的获奖作品...
...答案是写一个Rakefile包含:
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
Run Code Online (Sandbox Code Playgroud)
然后输入即可$ rake。所有测试都运行,而我的手动测试数据和配置都保留下来。
我们需要 Ruby Rakefile,而不是 a Makefile,因为我们需要处理输出并查找错误以成功将 0 或 1 返回到环境。但adb etc不符合“测试错误是语法错误”的规则,并且不会在错误时传播正确的退出值。我希望我的命令行异常终止并且不提交损坏的代码,因此我们将测试输出存储到 SERIALNUMBER.txt 文件中,然后存储到grep它们中。
而且我们需要线程,这样很多设备就可以同时测试,而无需互相等待。
正确检测错误可以将其集成到一行中$ rake && git commit -am 'the same comment every time' && git push。如果有任何错误,它们会显示在控制台上,并且git commit不会发生。
我们需要Makefile-风格的处理,因为如果任何命令失败,我们需要停止处理,遵循“测试错误是语法错误”的规则。
(一个新的错误是在 Espresso 测试故障时,我们不再收到说明 HTML 输出文件在哪里的消息,但因为错误本身就在喷射中,所以这不是什么问题。)
另一个错误:如果我通过 USB 调试连接了两台平板电脑,我会得到error: more than one device/emulator,这完全是假的,因为./gradlew test connectedAndroidTest自然可以在所有设备上正常工作......这就是为什么必须Rakefile找到所有设备然后分派adb shell -s给每个设备。
Rakefile请在此处写下有关使用低级电话的所有投诉gradlew:[__]。
| 归档时间: |
|
| 查看次数: |
110 次 |
| 最近记录: |