gpt4 book ai didi

android - 在 Android Studio 的单元测试功能中获取 AndroidTestCase 或 InstrumentationTestCase 的上下文

转载 作者:IT老高 更新时间:2023-10-28 22:23:25 40 4
gpt4 key购买 nike

我的一些旧测试使用 Android Studio 1.1.0 的新单元测试支持功能运行。运行 gradlew testDebug 时,测试会运行,但所有需要 Context 的测试都会失败,因为 getContext (AndroidTestCase)/getInstrumentation.getContext() (InstrumentationTestCase) 都返回 null .

我该如何解决这个问题?

这是我尝试过的两种变体:

import android.content.Context;
import android.test.InstrumentationTestCase;

public class TestTest extends InstrumentationTestCase {

Context context;

public void setUp() throws Exception {
super.setUp();

context = getInstrumentation().getContext();

assertNotNull(context);

}

public void testSomething() {

assertEquals(false, true);
}

}

import android.content.Context;
import android.test.AndroidTestCase;

public class TestTest extends AndroidTestCase {

Context context;

public void setUp() throws Exception {
super.setUp();

context = getContext();

assertNotNull(context);

}

public void testSomething() {

assertEquals(false, true);
}

}

这是我模块的build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

testOptions {
unitTests.returnDefaultValues = true
}

defaultConfig {
applicationId "com.example.test.penistest"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
testCompile 'junit:junit:4.12'
}

这里是项目的 build.gradle:

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

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'

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

allprojects {
repositories {
jcenter()
}
}

编辑:在升级到 AS 1.1.0 并在设备/模拟器上运行之前,我的测试全部运行。

编辑:

这里有2张InstrumentationTestCase和AndroidTestCase失败的截图:

enter image description here

enter image description here

最佳答案

更新 - 请使用 Espresso 编写仪器测试

较新的示例:

我让这些在没有部署到设备的情况下工作。将 测试放在 /src/main/test/ 文件夹中。

这是较新的示例,我采用了您的示例并在我自己的临时测试项目中对其进行了测试。我通过命令行运行测试:./gradlew clean test。请在此处阅读更多信息:https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support .

顶部build.gradle:

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

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'

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

allprojects {
repositories {
jcenter()
}
}

应用build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
applicationId "com.test"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

testOptions { // <-- You need this
unitTests {
returnDefaultValues = true
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'

testCompile 'junit:junit:4.12' // <-- You need this
}

基本测试:

InstrumentationTestCaseTest 来测试 ContextAssertions

import android.content.Context;
import android.test.InstrumentationTestCase;
import android.test.mock.MockContext;

public class InstrumentationTestCaseTest extends InstrumentationTestCase {

Context context;

public void setUp() throws Exception {
super.setUp();

context = new MockContext();

assertNotNull(context);

}

public void testSomething() {

assertEquals(false, true);
}

}

ActivityTestCase 来测试你的 Resources

import android.content.Context;
import android.content.res.Resources;
import android.test.ActivityTestCase;

public class ActivityTestCaseTest extends ActivityTestCase {

public void testFoo() {

Context testContext = getInstrumentation().getContext();
Resources testRes = testContext.getResources();

assertNotNull(testRes);
assertNotNull(testRes.getString(R.string.app_name));
}
}

AndroidTestCase 来测试 ContextAssertions

import android.content.Context;
import android.test.AndroidTestCase;
import android.test.mock.MockContext;

public class AndroidTestCaseTest extends AndroidTestCase {

Context context;

public void setUp() throws Exception {
super.setUp();

context = new MockContext();

setContext(context);

assertNotNull(context);

}

// Fake failed test
public void testSomething() {
assertEquals(false, true);
}
}

谷歌搜索旧示例:

在谷歌搜索了很多关于这个错误之后,我相信你的赌注是使用 getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res). 或类似的东西为了测试你的资源。

使用 InstrumentationTestCase:

测试资源:

public class PrintoutPullParserTest extends InstrumentationTestCase {

public void testParsing() throws Exception {
PrintoutPullParser parser = new PrintoutPullParser();
parser.parse(getInstrumentation().getContext().getResources().getXml(R.xml.printer_configuration));
}
}

来源:https://stackoverflow.com/a/8870318/950427https://stackoverflow.com/a/16763196/950427

使用 ActivityTestCase:

测试资源:

public class Test extends ActivityTestCase {

public void testFoo() {

// .. test project environment
Context testContext = getInstrumentation().getContext();
Resources testRes = testContext.getResources();
InputStream ts = testRes.openRawResource(R.raw.your_res);

assertNotNull(testRes);
}
}

来源:https://stackoverflow.com/a/9820390/950427

使用 AndroidTestCase:

获取 Context(一个简单的 hack):

private Context getTestContext() {
try {
Method getTestContext = ServiceTestCase.class.getMethod("getTestContext");
return (Context) getTestContext.invoke(this);
} catch (final Exception exception) {
exception.printStackTrace();
return null;
}
}

来源:https://stackoverflow.com/a/14232913/950427

但是,如果你查看AndroidTestCase的源代码,看起来你需要自己设置一个Context:

来源:http://alvinalexander.com/java/jwarehouse/android/core/java/android/test/AndroidTestCase.java.shtml

关于android - 在 Android Studio 的单元测试功能中获取 AndroidTestCase 或 InstrumentationTestCase 的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28960898/

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