标签: android-instrumentation

风味和仪器测试 - gradle 配置

我在我的项目中使用口味,并且我想添加特定于每种口味的仪器测试。

所以我创建了

MyApplication/src/androidTestFlavor1/java/com.package.test
MyApplication/src/androidTestFlavor2/java/com.package.test
Run Code Online (Sandbox Code Playgroud)

但它无法正常工作。

所以,我尝试在 build.gradle 中配置它,我添加了

android {
    ...

    sourceSets {
        flavor1{
            instrumentTest.setRoot('src/instrumentationTestFlavor/java')
        }
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

错误:(59, 0) 在源集 ding 上找不到属性“instrumentTest”。

这里最好的解决方案是什么?

android robotium build.gradle android-instrumentation

2
推荐指数
1
解决办法
2075
查看次数

运行 Junit 5 Android 测试时未解析的引用 jupiter 和 assertTrue

我正在尝试在 Kotlin 中创建一个 Android 测试类来处理,Bitmap但由于这些错误,我无法运行测试。它们仅发生在androidTest中的任何测试类中,但测试中的简单 JVM 测试运行没有问题。

首先,这就是我的测试类的样子

import android.graphics.Bitmap
import androidx.test.core.app.ApplicationProvider
import org.junit.jupiter.api.Assertions.assertTrue

class RoundImageTest {

    @org.junit.jupiter.api.Test
    fun imagesRatio() {
        // test with square images
        val squareBitmap: Bitmap = Bitmap.createBitmap(
             164, 164, Bitmap.Config.ARGB_8888
        )
        assertTrue(squareBitmap.height == squareBitmap.width)
    }
}
Run Code Online (Sandbox Code Playgroud)

按照此处指定的说明,我将其放入项目的build.gradle中

dependencies {
    classpath 'com.android.tools.build:gradle:4.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'eu.appcom.gradle:android-versioning:1.0.2'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.10.0"
    classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"
    classpath("de.mannodermaus.gradle.plugins:android-junit5:1.6.2.0")
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-instrumentation junit5

2
推荐指数
1
解决办法
4525
查看次数

androidTestCompile在Android Studio 2.1.2中不起作用

我按照这里的说明添加了一个检测测试,但不幸的是Android Studio抱怨测试运行器包(com.android.support.test:runner:0.5)或测试规则或espresso-core(cannot find symbol)中的类.如果我将依赖关系类型从更改androidTestCompilecompile,则错误消失.我创建了一个检测运行配置,并且当前选择了运行配置.

编辑:这是我们的Gradle构建文件:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'realm-android'
apply plugin: 'pmd'

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        mavenCentral()
        maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:2.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
        classpath 'io.realm:realm-gradle-plugin:1.0.0'
        classpath 'io.fabric.tools:gradle:1.21.6'
    }

    configurations.classpath.exclude group: 'com.android.tools.external.lombok'

}

repositories {
    jcenter()
    mavenCentral()
    maven { …
Run Code Online (Sandbox Code Playgroud)

junit android android-instrumentation

1
推荐指数
1
解决办法
1634
查看次数

如何在Android unitTest/AndroidTest中测试文件IO

我正在努力改进我的项目的代码覆盖率,因为我写了一个方法,internalStorage通过使用Android Developer网站的以下代码片段将文件写入android .

String FILENAME = "hello_file";
String string = "hello world!";
File testFile = new File(context.getFilesDir(), FILENAME);
FileOutputStream fos =new FileOutputStream(file);
fos.write(string.getBytes());
fos.close();
Run Code Online (Sandbox Code Playgroud)

我的想法是通过读取文件并与之进行比较hello world!并查看它们是否匹配来证明我的书写功能在单元测试/ Android Instrumentation测试中有效.但是,由于以下原因,对我来说测试这个并不是一件容易的事

  1. 我不知道单元测试方面(JVM)的文件路径
  2. 不是从Android仪器测试的角度来看.

在Android中测试此类IO功能的最佳做法是什么?我是否应该关心文件是否已创建并放入?或者我只是检查一下是否为fos空?

FileOutputStream fos =new FileOutputStream(file);
Run Code Online (Sandbox Code Playgroud)

请给我提供建议.谢谢.

android unit-testing android-instrumentation

1
推荐指数
1
解决办法
1870
查看次数

在 androidTest 文件夹中找不到 jUnit,但在 test 文件夹中找到

我正在 android 中编写仪器测试,但是当我导入jUnit(使用@RunWith注释)时,它无法找到 jUnit,但我已经在单元测试中(在test文件夹下)使用 jUnit 。我什至无法开始编写测试。
同样的事情也发生在AndroidJUnit4课堂上。

我已将这些内容包含在build.gradle

testCompile brazilGradle.testbuild('junit')

androidTestCompile brazilGradle.testbuild('AndroidSupportTestPackage-rules')
androidTestCompile brazilGradle.testbuild('AndroidSupportTestPackage-runner')
Run Code Online (Sandbox Code Playgroud)

我也在 defaultConfig 中包含了这个:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Run Code Online (Sandbox Code Playgroud)

为什么它在一个文件夹中工作而不在另一个文件夹中工作?

android junit4 android-testing android-instrumentation

1
推荐指数
1
解决办法
321
查看次数

使用 Koin 进行正确的仪器测试

无法让这个东西正常工作。

  1. 我在测试运行程序下注册了自定义测试应用程序:
class HelloInstrumentationTestRunner : AndroidJUnitRunner() {
    override fun newApplication(
        cl: ClassLoader?, className: String?, context: Context?
    ): Application {
        return Instrumentation.newApplication(HelloTestApp::class.java, context)
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 我的应用程序实例像往常一样启动:
        startKoin {
            androidLogger()
            androidContext(applicationContext)
            fragmentFactory()
            modules(appModule + viewModelsModule)
        }
Run Code Online (Sandbox Code Playgroud)
  1. 问题 1:在我的仪器测试中,我无法做到stopKoin()(说没有配置 Koin 上下文。请使用 startKoin 或 koinApplication DSL)
  2. 问题 2:当我尝试使用 @After 中的 unloadKoinModules/loadKoinModules 解决这种情况时,我的declareMock后续测试方法不再起作用。

所有这些问题基本上都是因为应用程序实例在测试之间仍然存在,因此 Android 应用程序实例内部配置的图形也在测试之间仍然存在。我需要这种情况不要发生,或者至少能够在测试之间修改图表。

android android-instrumentation koin

1
推荐指数
1
解决办法
2125
查看次数

如何使用命令行在 Android 测试类中传递参数?

我想要一种将命令行参数发送到我的测试类以运行所有单元测试并从中创建测试覆盖率报告的方法。我使用下面的命令来生成测试覆盖率报告并传递参数

./gradlew createDebugCoverageReport -Dparam=input1  
Run Code Online (Sandbox Code Playgroud)

下面是应用程序 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: …
Run Code Online (Sandbox Code Playgroud)

android gradle android-testing build.gradle android-instrumentation

1
推荐指数
1
解决办法
2518
查看次数

Android Studio + Gradle 和 AndroidTest res/raw

我有一个安卓应用。我想编写仪器测试。

我想将一些特定文件放在res/raw测试 apk的文件夹中。

我将它们放在androidTest/src/res/raw文件夹中,并在代码中使用R.raw.file. 但是,该应用程序引用了一些其他资源文件,该文件在我的main源集中。

如何确保我的测试 apk 从其自己的res/raw文件夹中获取正确的文件?

android android-instrumentation

0
推荐指数
1
解决办法
2499
查看次数