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

asc*_*sco 22 android unit-testing android-studio

我使用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);
    }  

}
Run Code Online (Sandbox Code Playgroud)

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的模块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'
}
Run Code Online (Sandbox Code Playgroud)

在这里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()
    }
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

Heres 2个失败的InstrumentationTestCase和AndroidTestCase的截图:

在此输入图像描述

在此输入图像描述

Jar*_*ows 43

更新 - 请使用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()
    }
}
Run Code Online (Sandbox Code Playgroud)

应用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
}
Run Code Online (Sandbox Code Playgroud)

基础测试:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

谷歌搜索旧例子:

谷歌搜索了很多这个错误,我相信你的赌注是使用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));
    }
}
Run Code Online (Sandbox Code Playgroud)

来源:https://stackoverflow.com/a/8870318/950427/sf/answers/1173423751/

使用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);
   }    
}
Run Code Online (Sandbox Code Playgroud)

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

使用AndroidTestCase:

得到Context(一个简单的黑客):

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

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

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

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


tay*_*yen 17

使用Android测试支持库,您可以

  • 获取测试apk上下文 InstrumentationRegistry.getContext()
  • 获取应用apk上下文 InstrumentationRegistry.getTargetContext()
  • 得到仪表 InstrumentationRegistry.getInstrumentation()

请参阅链接页面的底部,了解如何将Testing Support库添加到项目中.

  • @IgorGanapolsky在运行Android测试时安装了两个APK。一个是您的应用程序的APK,第二个是测试的APK。因此,因此“应用APK上下文”和“测试APK上下文”。 (2认同)

JJD*_*JJD 11

这是设置(单元)仪器测试的最新方法

建立

build.gradle中添加:

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

和以下依赖项:

// Instrumentation tests
androidTestImplementation "com.android.support.test:runner:$supportTestRunnerVersion"
// To use assertThat syntax
androidTestImplementation "org.assertj:assertj-core:$assertJVersion"
Run Code Online (Sandbox Code Playgroud)

示例类

public class Example {

    public Object doSomething() {
        // Context is used here
    }

}
Run Code Online (Sandbox Code Playgroud)

示例测试

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(AndroidJUnit4.class)
public class ExampleTest {

    private Context context;

    @Before
    public void setUp() {
        // In case you need the context in your test
        context = InstrumentationRegistry.getTargetContext();
    }

    @Test
    public void doSomething() {
        Example example = new Example();
        assertThat(example.doSomething()).isNotNull();
    }

}
Run Code Online (Sandbox Code Playgroud)

文档

  • 这应该是正确的答案,因为它包含获取检测数据的最新正确方法. (3认同)
  • API 更改为 Context context = androidx.test.core.app.ApplicationProvider.getApplicationContext() (2认同)