gpt4 book ai didi

android - 如何在本地单元测试期间访问资源?

转载 作者:行者123 更新时间:2023-12-05 00:04:43 24 4
gpt4 key购买 nike

有没有办法在单元测试期间访问资源?我尝试了在 StackOverflow 中找到的许多可能性,但都失败了。

我最后一次尝试(与下面的代码匹配)给我这个任务 testReleaseUnitTest 的错误:

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)
at fr.alpha.calculator.TestOperationManager.testAddCharacterEmptyOperation(TestOperationManager.java:20)

仪器是通过仪器测试创建的,对吧?是否可以避免这种情况以加快测试速度?

感谢阅读!

我的测试课:

package fr.alpha.calculator;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.Test;

import android.content.Context;
import android.content.res.Resources;
import androidx.test.core.app.ApplicationProvider;

import fr.alpha.calculator.OperationManager;

public class TestOperationManager{

@Test
public void testAddCharacterEmptyOperation(){

final Context context = ApplicationProvider.getApplicationContext();
final Resources res = context.getResources();

final OperationManager testOperationManager = new OperationManager(res);

assertTrue(testOperationManager.addCharacter('0'),
"addCharacter must return true !");

final OperationManager expectedOperationManager =
new OperationManager(res);
expectedOperationManager.setOperation("0");

assertEquals(expectedOperationManager, testOperationManager,
"operation must be equal to \"0\" !");

}
}

我的app/build.gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "fr.alpha.calculator"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

testOptions{
unitTests.all{
useJUnitPlatform()
}
}
}

dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'org.apache.commons:commons-lang3:3.10'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testImplementation 'androidx.test:core:1.2.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}

最佳答案

我认为这不是一个很好的解决方案,但我没有使用 Resources 来获取我想要的字符串,而是将它简单地放在测试类中的 String 变量中。我希望有人有比我更好的答案哈哈

关于android - 如何在本地单元测试期间访问资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62557519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com