android - 如何使用命令行在 Android 测试类中传递参数?
问题描述
我想要一种将命令行参数发送到我的测试类以运行所有单元测试并从中创建测试覆盖率报告的方法。我正在使用以下命令生成测试覆盖率报告并传递参数
./gradlew createDebugCoverageReport -Dparam=input1
下面是app gradle文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example.testcoverageapp"
minSdkVersion 27
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
testOptions {
unitTests.includeAndroidResources = true
unitTests.all {
useJUnitPlatform()
test {
println systemProperties['param']
systemProperties(System.getProperties())
}
}
}
tasks.withType(Test){
systemProperties=System.properties
println systemProperties['param.name']
systemProperty 'param.name', System.getProperty('param')
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
}
我正在使用下面的方法来访问命令行参数。
val uName : String? = System.getProperty("param")
if(uName !=null){
Log.d("ExampleInstrumentedTest", "Argument param: " + uName)
}
但是,测试文件中没有收到命令行参数,它在日志中打印空值。
/com.example.testcoverageapp D/ExampleInstrumentedTest: Argument param: null
我尝试了许多在线文章来接收测试文件中的命令行参数,但可能是我遗漏了一些东西。请帮忙。
解决方案
我发现下面的方法将命令行参数传递给 Android 测试文件。参考:Gradle 命令行参数以覆盖 build.gradle 中的属性
步骤 1:创建要在 gradle.properties 文件中传递的参数
TEST_PARAM1=param1
TEST_PARAM2=param2
第 2 步:在 defaultconfig 中的 app build.gradle 文件中添加参数条目,并从 gradle.properties 文件中添加参数名称的引用。
android {
...
defaultConfig {
...
buildConfigField "String", "TEST_PARAM1", "\"$TEST_PARAM2\""
buildConfigField "String", "TEST_PARAM2", "\"$TEST_PARAM2\""
}
第 3 步:构建测试项目,因此 BuildConfig.java 使用默认值创建。
public static final String TEST_PARAM1 = "param1";
public static final String TEST_PARAM2 = "param2";
第 4 步:现在使用 -P 和参数运行您的测试覆盖率命令(或任何其他 gradle 命令),如下所示
./gradlew createDebugAndroidTestCoverageReport -PTEST_PARAM1=param_value1 -PTEST_PARAM2=param_value2
使用上述命令,BuildConfig.java 将自动更新为以下更改。
public static final String TEST_PARAM1 = "param_value1";
public static final String TEST_PARAM2 = "param_value2";
第 5 步:现在在您想要的测试文件中访问 BuildConfig.java 中创建的参数,如下所示。
val uName : String? = BuildConfig.TEST_PARAM1
val uPASS : String? = BuildConfig.TEST_PARAM2
希望这对任何偶然发现这里的人有所帮助。谢谢。
推荐阅读
- php - PHP Laravel - 字段列表中的未知列
- python - 我无法导入pyaudio?
- c# - 获取adobe reader打开的文件路径
- java - Java Generics super/extends - 不同的行为(用于 OCP java 8 学习目的)
- mysql - 使用 mySQL 和 Mongodb 插入节点循环
- angularjs - 将代码重构为 momentjs 的一个函数
- javascript - OLOO 创造差异?
- java - 下一行间距问题
- javascript - React 中的状态应该提升多高?
- javascript - 链接在 Chrome 中不起作用,但在 firefox 和 safari 中有效